我在这里阅读了其他问题,发现当编译器抛出Cannot find symbol
问题Collections.sort(List<T> list)
时,最常见的是......
- 它没有通过
List<T>
List<T>
不执行Comparable
java.util.Collection
被遗忘的进口
我已经完成了所有这些事情,所以我怀疑我的实现在某种程度上是错误的。根据这个堆栈溢出条目和手册,我的实现应该是合法的,所以我没有想法。如果 sort 被传递,规则会改变List<Item>
吗?
可比实施
public abstract class Item implements Comparable<Item>
9 {
...//Fields and constructor omitted
25 @Override
26 public int compareTo(Item i)
27 {
28 // String title = this.title; DEBUG FLAG: delete maybe?
29 return this.title.compareTo(i.title); //Returns a negative value if title < i.title, implements alphabetical order by title
30 }
调用 Library.java(假设正确构建的 LinkedList 的 TreeMap)
public Collection<Item> itemsForKeyword(String keyword)
25 {
26 List<Item> list;
27 if(keywordDbase.get(keyword) == null) //There is no mapping at specified keyword
28 {
29 list = null;
30 }
31 else if(keywordDbase.get(keyword).isEmpty()) //There is a mapping but it is empty
32 {
33 list = null;
34 }
35 else //There is a list that has at least one item in it
36 {
37 list = keywordDbase.get(keyword); //stores a reference to the LinkedList in list
38 }
39
40 Collections.sort(list); //DEBUG FLAG: Calling sort may be unnecessary
41
42 return list; here
43 }
错误
library/Library.java:40: error: cannot find symbol
Collections.sort(list);
^