6

使用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 行:shuffle(List<String[]>)> 类型集合的方法未定义

4

3 回答 3

7

引自GWT 的 JRE 仿真参考

Google Web Toolkit 包含一个模拟 Java 运行时库子集的库。下面的列表显示了 GWT 可以自动翻译的一组 JRE 包、类型和方法。请注意,在某些情况下,给定类型仅支持方法的子集。

具体来说,如果您查看Package java.utilCollections  ,您会发现它不包含该方法。shuffle()

于 2012-04-07T08:44:00.663 回答
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));  
}
于 2014-03-20T13:34:35.023 回答
3

除了matsev已经说过的话:

如果你的代码是 GPL,你可以复制 SUN 的实现:

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并进行了一些优化,以防列表不是随机访问。

于 2012-07-18T08:42:05.860 回答