黑马程序员技术交流社区
标题:
为什么我这个代码不能进行年龄排序呢
[打印本页]
作者:
小洋人最happy
时间:
2012-11-28 12:13
标题:
为什么我这个代码不能进行年龄排序呢
import java.util.*;
class TreeSetTest2
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi01",11));
ts.add(new Student("lisi03",51));
ts.add(new Student("lisi04",16));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu = (Student)it.next();
sop(stu.getName()+"...."+stu.getAge());
}
}
}
class Student implements Comparable
{
private String name;
private int age;
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.name == s.name)
return 1;
if(this.name == s.name)
{
return this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
作者:
黑马_郑亮新
时间:
2012-11-28 12:29
import java.util.*;
class TreeSetTest
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi01",11));
ts.add(new Student("lisi03",51));
ts.add(new Student("lisi04",16));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu = (Student)it.next();
sop(stu.getName()+"...."+stu.getAge());
}
}
}
class Student implements Comparable
{
private String name;
private int age;
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 this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
因为你没有指定需要比较的是年龄
作者:
坚持远方
时间:
2012-11-28 12:35
你的compareto代码写错了,应该是这样:
public int compareTo(Object obj)
{
//return 0;
if(!(obj instanceof Student))
throw new RuntimeException("不是学生对象");
Student s = (Student)obj;
System.out.println(this.name+"....compareto....."+s.name);
if(this.age>s.age)
return 1;
if(this.age==s.age)
{
return this.name.compareTo(s.name);
}
return -1;
/**/
}//这个是毕老师写的那个代码,是先按年龄排序,年龄相等再按姓名排序,你如果想先按姓名排序,换一下就行
作者:
张海涛
时间:
2012-11-28 15:55
这样写就OK了:
import java.util.*;
class TreeSetTest
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi01",11));
ts.add(new Student("lisi03",51));
ts.add(new Student("lisi04",16));
Iterator it = ts.iterator();
while (it.hasNext())
{
Student stu = (Student)it.next();
sop(stu.getName()+"...."+stu.getAge());
}
}
}
class Student implements Comparable
{
private String name;
private int age;
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 this.name.compareTo(s.name);
}
return -1;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2