import java.util.*;
class Example
{
public static void main(String args[])
{
TreeSet<Student> mytree=
new TreeSet<Student>(new Comparator<Student>()
{
public int compare(Student a,student b)
{
return a.compareTo(b);
}
});
Student st1,st2,st3,st4;
st1=new Student(90,"zhan ying");
st2=new Student(66,"wang heng");
st3=new Student(86,"zan ying");
st4=new Student(76,"hang heng");
mytree.add(st1);
mytree.add(st2);
mytree.add(st3);
mytree.add(st4);
Iterator<Student> te=mytree.iterator();
while(te.hasNext())
{
Student stu=te.next();
System.out.println(""+stu.name+""+stu.english);
}
}
}
class Student implements Comparable
{
int english =0;
String name;
Student(int e,String n)
{
english=e;name=n;
}
public int compareTo(Object b)
{
Student st=(Student)b;
return(this.english-st.english);
}
} 咋整的,错哪儿了?哪位给个正确的answer,第七行哪错了?俺刚学TreeSet
class Example
{
public static void main(String args[])
{
TreeSet<Student> mytree=
new TreeSet<Student>(new Comparator<Student>()
{
public int compare(Student a,student b)
{
return a.compareTo(b);
}
});
Student st1,st2,st3,st4;
st1=new Student(90,"zhan ying");
st2=new Student(66,"wang heng");
st3=new Student(86,"zan ying");
st4=new Student(76,"hang heng");
mytree.add(st1);
mytree.add(st2);
mytree.add(st3);
mytree.add(st4);
Iterator<Student> te=mytree.iterator();
while(te.hasNext())
{
Student stu=te.next();
System.out.println(""+stu.name+""+stu.english);
}
}
}
class Student implements Comparable
{
int english =0;
String name;
Student(int e,String n)
{
english=e;name=n;
}
public int compareTo(Object b)
{
Student st=(Student)b;
return(this.english-st.english);
}
} 咋整的,错哪儿了?哪位给个正确的answer,第七行哪错了?俺刚学TreeSet
解决方案 »
- android 开发wenservice问题(java.rmi.RemoteException cannot be resolved)
- java的io中的文件关闭问题!
- java编写坦克大战遇到的问题
- java对象 正解
- 新手问个问题,请大家帮忙看看
- [求助]有个疑问
- 为什么说没有main函数
- 照着<<CORE JAVA>>中打的CODE,系统提示出错,请大家帮忙找一下?
- 在线等待,马上结贴
- 为什么JAVA程序中的字体那么难看,难道它不是用Windows的字库?
- JTable和CellEditor焦点转移和接受修改的问题,请大家支招
- jxl如何得到一个excel中一个合并的单元格到底占多少行,多少列呢 ?
student change Student
running,result:
wang heng66
hang heng76
zan ying86
zhan ying90
应该是
public int compare(Student a,Student b) 吧??
我想用TreeSet来实现自动比较按小到大输出。
public class Example
{
public static void main(String args[])
{
TreeSet mytree=new TreeSet(){
public int compare(Student a,Student b)
{
return a.compareTo(b);
}
}
;
Student st1,st2,st3,st4;
st1=new Student(90,"zhan ying");
st2=new Student(66,"wang heng");
st3=new Student(86,"zan ying");
st4=new Student(76,"hang heng");
mytree.add(st1);
mytree.add(st2);
mytree.add(st3);
mytree.add(st4);
Iterator te=mytree.iterator();
while(te.hasNext())
{
Object st=te.next();
Student stu=(Student)st;
System.out.println("|"+stu.name +"|"+stu.english);
System.exit(0);
}
}
}
class Student implements Comparable
{
int english =0;
String name;
Student(int e,String n)
{
english=e;name=n;
}
public int compareTo(Object b)
{
Student st=(Student)b;
return(this.english-st.english);
}
}