黑马程序员技术交流社区
标题:
运行结果 求解
[打印本页]
作者:
宋耀冬
时间:
2013-4-5 13:13
标题:
运行结果 求解
本帖最后由 宋耀冬 于 2013-4-5 17:48 编辑
import java.util.*;
class TreeSetDemo {
public static void main (String[] args) {
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02", 22));
ts.add(new Student("lisi007", 20));
ts.add(new Student("lisi09", 19));
ts.add(new Student("lisi08", 19));
//ts.add(new Student("lisi01", 40));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student stu = (Student)it.next();
System.out.println(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;
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;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
复制代码
我的运行结果是这样的
lisi02...compareto...lisi02
lisi007...compareto...lisi02
lisi09...compareto...lisi02
lisi09...compareto...lisi007
lisi08...compareto...lisi007
lisi08...compareto...lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22
lisi02 自身进行了一次比较 老师的视频上没有啊 困惑 求解
作者:
ㄨ____陌生
时间:
2013-4-5 13:35
运行结果是 lisi007....compareto.....lisi02
lisi09....compareto.....lisi02
lisi09....compareto.....lisi007
lisi08....compareto.....lisi007
lisi08....compareto.....lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22
你重新运行下试试、、
作者:
田光峰
时间:
2013-4-5 14:15
import java.util.*;
class TreeSetDemo {
public static void main (String[] args) {
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02", 22));
ts.add(new Student("lisi007", 20));
ts.add(new Student("lisi09", 19));
ts.add(new Student("lisi08", 19));
//ts.add(new Student("lisi01", 40));
Iterator it = ts.iterator();
while(it.hasNext()) {
Student stu = (Student)it.next();
System.out.println(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;
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;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
我的运行结果是
lisi007....compareto.....lisi02
lisi09....compareto.....lisi02
lisi09....compareto.....lisi007
lisi08....compareto.....lisi007
lisi08....compareto.....lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22
你在运行一次试试
作者:
jiangnan
时间:
2013-4-5 14:42
运行了一下,跟上面两位的结果一样,是不是LZ多加了一行 ts.add(new Student("lisi02", 22));
作者:
lcjasas
时间:
2013-4-5 15:32
本帖最后由 lcjasas 于 2013-4-5 15:33 编辑
)~PU@CA@278MN9@TI8YWYSC.jpg
(21.61 KB, 下载次数: 11)
下载附件
2013-4-5 15:32 上传
我的输出结果也会多出lisi02....compareto.....lisi02
不知道是不是jdk版本不一样的原因。
作者:
宋耀冬
时间:
2013-4-5 16:54
我用的JDK 1.7 你们呢
作者:
〃Mr.Zぐ
时间:
2013-4-5 17:18
lisi007...compareto...lisi02
lisi09...compareto...lisi02
lisi09...compareto...lisi007
lisi08...compareto...lisi007
lisi08...compareto...lisi09
lisi08...19
lisi09...19
lisi007...20
lisi02...22
我的运行结果是这样的
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2