像我一样使用import java.util.Collections;
。不是GWT的。让GWT项目的共享文件夹中包含错误的类。
List<String []> qaList;
qaList = new ArrayList<String[]>();
qaList.add("12345 main st", "tomah");
qaList.add("124 main st", "lacrosse");
qaList.add("123 main", "yeeehahaaa");
Collections.shuffle(qaList);
[错误] [_012cfaexam] - 第109行:&gt;类型集合
未定义方法shuffle(List<String[]>)
答案 0 :(得分:7)
引自GWT's JRE Emulation Reference:
Google Web Toolkit包含一个模拟Java运行时库子集的库。下面的列表显示了GWT可以自动转换的JRE包,类型和方法的集合。请注意,在某些情况下,给定类型仅支持方法的子集。
具体来说,如果您查看Package java.util中的Collections
,您会发现它不包含shuffle()
方法。
答案 1 :(得分:4)
还有另一种方法可以解决这个问题:
Random random = new Random(qaList.size());
for(int index = 0; index < qaList.size(); index += 1) {
Collections.swap(qaList, index, index + random.nextInt(qaList.size() - index));
}
答案 2 :(得分:3)
除了matsev已经说过的内容:
如果您的代码是GPL,则只需复制SUNs实施:
public static void shuffle(List<?> list, Random rnd) {
int size = list.size();
if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
for (int i=size; i>1; i--)
swap(list, i-1, rnd.nextInt(i));
} else {
Object arr[] = list.toArray();
// Shuffle array
for (int i=size; i>1; i--)
swap(arr, i-1, rnd.nextInt(i));
// Dump array back into list
ListIterator it = list.listIterator();
for (int i=0; i<arr.length; i++) {
it.next();
it.set(arr[i]);
}
}
}
基本上Fisher Yates shuffle有一些优化,以防列表不是随机访问。