黑马程序员技术交流社区

标题: TreeSet集合存储自定义对象的问题 [打印本页]

作者: 冯超    时间: 2012-8-22 17:43
标题: TreeSet集合存储自定义对象的问题
这段代码没有报错,为什么编译出错了
package javatest2;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args)
    {
        TreeSet ts = new TreeSet();
        ts.add(new student("lis1", 1));
        ts.add(new student("lis2", 2));
        ts.add(new student("lis3", 3));
        Iterator it = ts.iterator();
        
        while(it.hasNext())
        {
            student t = (student)it.next();
            System.out.println(t.getName() + "..." + t.getAge());
        }
        
    }
   
}
class student implements Comparable
{
    private String name;
    private int age;
    student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Object obj)
    {
        if(!(obj instanceof student))
            throw new RuntimeException("非学生对象");
        student s = (student)obj;
        if(this.age > age)
            return 1;
        else if(this.age == age)
            return 0;
        else
            return -1;
        
        
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
   错误信息是:C:\Users\Administrator.KMNFCDZKTTHPEI8\.netbeans\7.1.2\var\cache\executor-snippets\run.xml:48:
Cancelled by user.
作者: 唐杨老师    时间: 2012-8-22 18:57
本帖最后由 唐杨 于 2012-8-22 19:08 编辑

代码是有问题的,而且最后还少了两个 大括号.} }

你的compareTo方法写的有问题.

所以导致你的程序运行结果,虽然age不同, 只有一个

lis1...1

   @Override
    public int compareTo(Object obj)
    {
        if(!(obj instanceof student))
            throw new RuntimeException("非学生对象");
        student s = (student)obj;
        if(this.age > age)                                  //这里应该是this.age > s.getAge() ;  (下边的是 this.age == s.getAge())    s.age才是真正的传入的参数的年龄
            return 1;
        else if(this.age == age)                         //这里 this.age 和 age 其实是同一个变量,所以永远相等
            return 0;
        else
            return -1;
    }
  1. 这个代码窗总容易丢东西,哎~~
复制代码

作者: 冯超    时间: 2012-8-22 19:01
语法就别说了·我没粘贴好·
  谢谢你拉
作者: 冯超    时间: 2012-8-22 19:05
唐杨 发表于 2012-8-22 18:57
代码是有问题的,而且最后还少了两个 大括号.} }

你的compareTo方法写的有问题.

package javatest2;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
    public static void main(String[] args)
    {
        TreeSet ts = new TreeSet();
        ts.add(new student("lis1", 1));
        ts.add(new student("lis2", 2));
        ts.add(new student("lis3", 3));
        Iterator it = ts.iterator();
        
        while(it.hasNext())
        {
            student t = (student)it.next();
            System.out.println(t.getName() + "..." + t.getAge());
        }
        
    }
   
}
class student implements Comparable
{
    private String name;
    private int age;
    student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    @Override
    public int compareTo(Object obj)
    {
        if(!(obj instanceof student))
            throw new RuntimeException("非学生对象");
        student s = (student)obj;
        if(this.age > s.age)
            return 1;
        else if(this.age == s.age)
            return 0;
        else
            return -1;
        
        
    }
    public String getName()
    {
        return name;
    }
    public int getAge()
    {
        return age;
    }
}
还是有问题啊·大哥
作者: 唐杨老师    时间: 2012-8-22 19:09
冯超 发表于 2012-8-22 19:05
package javatest2;

import java.util.Iterator;

不是s.age ,应该是 s.getAge()
我刚才弄错了,现在已经改过来了




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