关于Comparator的问题,求大神解答 exceptionarraylist 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public static void main(String[] args) throws IOException { List<Student> list = new ArrayList<Student>() ; list.add(new Student("123",22)) ; list.add(new Student("李四",13)); list.add(new Student("王五",12)); Collections.sort(list ,new Comparator<Student>(){// public int compareTo(Student stu1 ,Student stu2){// if(stu1.getAge()>stu2.getAge()){// return 1;// }else if(stu1.getAge() < stu2.getAge()){// return -1 ;// }else{// return stu1.getName.compareTo(stu2.getName());// }// } @Override public int compare(Student o1, Student o2) { if(o1.getAge()>o2.getAge()){ return 1; }else if(o1.getAge() < o2.getAge()){ return -1 ; }else{ return o1.getName().compareTo(o2.getName()); } } }); } static class Student{ private String name ; private int age ; public Student(){} public Student(String name ,int age ){ this.name = name ; this.age = age ; } public void setName(String name ){ this.name = name ; } public void setAge(int age){ this.age = age ; } public String getName(){ return this.name; } public int getAge(){ return this.age ; } } 错了一大堆,1:你的集合对象创建的是student,使用的却是list2.实现接口,没有实现方法3.定义了内部类,却不是静态,怎么在静态方法当中使用呢? Collections.sort(student ,new Comparator<Student>(){把student换成list然后在开始比较前先判断stu1 和stu2是不是为null,然后才是比较熟悉! 【求助】XStream序列化对象到xml 哪个大虾帮我改写下如下PHP代码 HQL子查询中怎么实现top功能 请教:一个类的方法调用另外一个类的方法,调用完毕后如何把结果值返回给原来的类呢? 求几个问题的答案~~~ 哪种语言编程效率最高? 100分解决复制整个目录 一个关于Java线程的问题 经验交流系列二:Java Pet Store Sample Application研究学习!!! 怎样使DataSource支持多个数据库?百分相赠! 求助cannot be resolved to a variable 一个递归父类树的问题,惭愧
List<Student> list = new ArrayList<Student>() ;
list.add(new Student("123",22)) ;
list.add(new Student("李四",13));
list.add(new Student("王五",12));
Collections.sort(list ,new Comparator<Student>(){
// public int compareTo(Student stu1 ,Student stu2){
// if(stu1.getAge()>stu2.getAge()){
// return 1;
// }else if(stu1.getAge() < stu2.getAge()){
// return -1 ;
// }else{
// return stu1.getName.compareTo(stu2.getName());
// }
// } @Override
public int compare(Student o1, Student o2) {
if(o1.getAge()>o2.getAge()){
return 1;
}else if(o1.getAge() < o2.getAge()){
return -1 ;
}else{
return o1.getName().compareTo(o2.getName());
}
}
});
}
static class Student{
private String name ;
private int age ;
public Student(){}
public Student(String name ,int age ){
this.name = name ;
this.age = age ;
}
public void setName(String name ){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age ;
}
}
错了一大堆,
1:你的集合对象创建的是student,使用的却是list
2.实现接口,没有实现方法
3.定义了内部类,却不是静态,怎么在静态方法当中使用呢?
然后在开始比较前先判断stu1 和stu2是不是为null,然后才是比较熟悉!