A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王桂丽 中级黑马   /  2012-7-27 12:05  /  1481 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王桂丽 于 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, 下载次数: 18)

未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

4 个回复

倒序浏览
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

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
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()语句都不成立的话就没有返回值。
回复 使用道具 举报
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, 下载次数: 17)

treeset.jpg

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
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,
虽然这个代码不会执行到,但只有这样编译器才觉得每个分支都覆盖到了,才不会报错!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马