黑马程序员技术交流社区

标题: TreeSet集合的细节问题 [打印本页]

作者: 王桂丽    时间: 2012-7-27 12:05
标题: TreeSet集合的细节问题
本帖最后由 王桂丽 于 2012-7-27 13:17 编辑

*
  需求:向TreeSet1中存储自定义对象学生。
  想按照学生的年龄进行排序。
*/
import java.util.*;
class TreeSet1
{
public static void main(String[] args)
{
  TreeSet tr=new TreeSet();
  tr.add(new Student("Lisi01",23));
  tr.add(new Student("Lisi02",24));
  tr.add(new Student("Lisi03",25));
  tr.add(new Student("Lisi04",26));
  Iterator it=tr.iterator();
  while (it.hasNext())
  {
   Student hs=(Student)it.next();
   System.out.println(hs.getName()+"……"+hs.getAge());
  
  }
}
  
  
}
class Student implements Comparable
{
private int age;
private String name;
Student(String name,int age)
{
  this.name=name;
  this.age=age;
}

public int compareTo(Object obj)
{
  if(!(obj instanceof Student))
   throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
  if(this.age>s.age)
   return 1;
  if(this.age==s.age)
   return 0;
  if(this.age<s.age)
   return -1;
  
}

public String getName()
{
  return name;
}
public int getAge()
{
  return age;
}
}





这是一段代码,不知道哪里出错,运行时总是提示没有返回值。检查了好几遍都没发现是哪里错了,帮忙看一下!

未命名.jpg (26.11 KB, 下载次数: 72)

未命名.jpg

作者: 张立江    时间: 2012-7-27 12:15
public int compareTo(Object obj)
{
  if(!(obj instanceof Student))
   throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
  if(this.age>s.age)
   return 1;
  if(this.age==s.age)
   return 0;
  if(this.age<s.age)
   return -1;


你把最后一个if语句去掉,直接写return -1
作者: 孙建飞    时间: 2012-7-27 12:42
public int compareTo(Object obj)
{
  if(!(obj instanceof Student))
   throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
  if(this.age>s.age)
   return 1;
  if(this.age==s.age)
   return 0;
  if(this.age<s.age)
   return -1;
  
  //在三个if()语句外再添加一个return语句就行了
}

//首先这个方法返回int类型,而你只是在if判断里面写了返回语句,也就是说你的假如你方法中的三个if()语句都不成立的话就没有返回值。
作者: 李菁    时间: 2012-7-27 13:14
public class Test2 {
        public static void main(String[] args) {
                TreeSet<Student> tr=new TreeSet<Student>(new Comparator<Student>() {

                        @Override
                        public int compare(Student o1, Student o2) {
                                // TODO Auto-generated method stub
                                return o1.getAge()-o2.getAge();
                        }
                       
                });
                tr.add(new Student("Lisi01",23));
                tr.add(new Student("Lisi02",24));
                tr.add(new Student("Lisi03",25));
                tr.add(new Student("Lisi04",26));
                for (Student student : tr) {
                           System.out.println(student.getName()+"\t"+"\t"+student.getAge());
                }
        }
}

不知道你想按年龄正序还是倒序。我是给你按正序排的。
如果想倒序排,只要把o1.getAge()-o2.getAge()变成o2.getAge()-o1.getAge();

treeset.jpg (14.28 KB, 下载次数: 64)

treeset.jpg

作者: 于星星    时间: 2012-7-27 14:16
public int compareTo(Object obj)
{
  if(!(obj instanceof Student))
   throw new RuntimeException("不是学生对象");
Student s=(Student)obj;
  if(this.age>s.age)
   return 1;
  if(this.age==s.age)
   return 0;
  if(this.age<s.age)
   return -1;
  return null;}
这里面存在可能没有覆盖到的分支,虽然你知道只有可能是>,< ,=三种情况,但程序不知道啊。
就像if...else if...else if ....后面一定要加个else一样。可以这样解决:在最后加上一个return null,
虽然这个代码不会执行到,但只有这样编译器才觉得每个分支都覆盖到了,才不会报错!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2