【发布时间】:2014-03-18 05:46:48
【问题描述】:
以下是我用于按预定义顺序对列表进行排序的代码。 itemsSorted 列表中提到了定义的顺序。
final List<String> itemsSorted = myMethod.getSortedItems();
List<String> plainItemList = myMethod2.getAllItems();
final Comparator<String> comparator = new Comparator<String>() {
public int compare(String str1, String str2) {
return orderOf(str1) - orderOf(str2);
}
private int orderOf(String name) {
return ((itemsSorted)).indexOf(name);
}
};
Collections.sort(plainItemList, comparator);
return plainItemList;
以上代码抛出
Caused by: java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList$1.set(Collections.java:1244)
at java.util.Collections.sort(Collections.java:221)
我不确定为什么该列表不可修改。请帮我解决这个问题。
【问题讨论】:
-
那么...
myMethod2.getAllItems()返回什么?您需要提供足够的信息来提供帮助。我们看不到您的代码。 -
这取决于列表的创建方式。给我们看
myMethod2.getAllItems()的代码 -
这不是 Collections$ UnmodifiableList $1 响铃吗?
-
那么 Cassandra 将返回一个不可修改的列表
-
为什么是不可变列表? stackoverflow.com/questions/16891659/…
标签: java sorting collections arraylist