【发布时间】:2017-03-21 13:53:11
【问题描述】:
我在这里阅读了其他问题,发现当编译器为Collections.sort(List<T> list) 抛出Cannot find symbol 时,问题通常是......
- 没有通过
List<T> -
List<T>没有实现Comparable - 忘记导入
java.util.Collection
我已经完成了所有这些事情,所以我怀疑我的实现在某种程度上是错误的。根据this 堆栈溢出条目和manual 我的实现应该是合法的,所以我没有想法。如果通过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);
^
【问题讨论】:
-
你需要导入
java.util.Collections。 -
天哪。如果您发布答案,我会将其标记为已接受@shmosel
标签: java collections cannot-find-symbol