请老师看下我的sort方法为啥那么会报错啊 我选的也是正确的那个啊
package com.imooc.sort; public class Student { private int num; private String name; private int age; public Student() { } public Student(int num, String name, int age) { super(); this.num = num; this.name = name; this.age = age; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return " [学号:" + num + ", 姓名:" + name + ", 年龄:" + age + "]"; } }
package com.imooc.sort; import java.util.Comparator; public class StudentComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { String name1=o1.getName(); String name2=o2.getName(); int n=name1.compareTo(name2); return n; } }
package com.imooc.set; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.imooc.sort.StudentComparator; public class StudentTest { public static void main(String[] args) { // TODO Auto-generated method stub Student one=new Student(40,"peter",20); Student two=new Student(28,"angle",5); Student three=new Student(35,"tom",18); List<Student> list=new ArrayList<Student>(); list.add(one); list.add(two); list.add(three); System.out.println("排序前"); for(Student student:list) { System.out.println(student); } Collections.sort(list,new StudentComparator); } }
0
收起
正在回答 回答被采纳积分+1
4回答
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星