解决方案 »

  1.   

    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,使用的却是list
    2.实现接口,没有实现方法
    3.定义了内部类,却不是静态,怎么在静态方法当中使用呢?
      

  2.   

     Collections.sort(student ,new Comparator<Student>(){把student换成list
    然后在开始比较前先判断stu1 和stu2是不是为null,然后才是比较熟悉!