【发布时间】:2015-05-11 11:09:12
【问题描述】:
我正在尝试通过Collections.sort 对字符串列表(将包含字母数字字符和标点符号)进行排序:
public class SorterDriver {
public static void main(String[] args) {
List<String> toSort = new ArrayList<String>();
toSort.add("fizzbuzz");
System.out.println("toSort size is " + toSort.size());
List<String> sorted = Collections.sort(toSort);
if(sorted == null) {
System.out.println("I am null and sad.");
} else {
System.out.println("I am not null.");
}
}
}
当我运行它时,我得到:
toSort size is 1
I am null and sad.
为什么为空?
【问题讨论】:
-
你确定代码可以编译吗?
Collections.sort()具有返回类型void... -
Collections.sort()的返回类型为void。它修改了它传递的List。 -
是的@Thomas - 我的实际代码在 Groovy 中,但我将其转换为 Java 以加快对问题的回答。似乎 Groovy 的动态特性掩盖了 Java 会立即暴露为编译器错误的内容。
标签: java list sorting collections