这段代码<>中的内容报错;检查了一下,还是找不到原因,请问老师是什么原因导致的?
//实现Comparator接口 public class StudentTest implements Comparator<Student>{ //实现接口中的方法 public int compare(Student one,Student two){ String name1 = one.getName(); String name2 = two.getName(); int n = name1.compareTo(name2); return n; } public static void main(String[] args){ //定义Student类的对象 Student one = new Student(40,20,"peter"); Student two = new Student(28,5,"angle"); Student three = new Student(35,18,"tom"); //将对象添加到List中 List<Student> list = new ArrayList<Student>(); list.add(one); list.add(two); list.add(three); //输出排序前的数据 for(Student s:list){ System.out.println(s); } //排序 Collections.sort(list,new StudentTest()); //输出排序后的数据 for(Student s:list){ System.out.println(s); } } }
正在回答 回答被采纳积分+1
StudentTest.java:2: error: cannot find symbol
public class StudentTest implements Comparator{
^
symbol: class Comparator
StudentTest.java:19: error: cannot find symbol
List list = new ArrayList();
^
symbol: class List
location: class StudentTest
StudentTest.java:19: error: cannot find symbol
List list = new ArrayList();
^
symbol: class ArrayList
location: class StudentTest
StudentTest.java:29: error: cannot find symbol
Collections.sort(list,new StudentTest());
^
symbol: variable Collections
location: class StudentTest
4 errors
报这个错误;
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星