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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 冯超 高级黑马   /  2012-8-22 17:43  /  2061 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

这段代码没有报错,为什么编译出错了
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.

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 新人提问,赞一个!

查看全部评分

4 个回复

倒序浏览
本帖最后由 唐杨 于 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. 这个代码窗总容易丢东西,哎~~
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1

查看全部评分

回复 使用道具 举报
语法就别说了·我没粘贴好·
  谢谢你拉
回复 使用道具 举报
唐杨 发表于 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:05
package javatest2;

import java.util.Iterator;

不是s.age ,应该是 s.getAge()
我刚才弄错了,现在已经改过来了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马