3
我有一段代码,它的想法是,它需要一个n数量的数组列表并将其洗牌50次,并且每次添加新的洗牌添加到另一个数组列表。Java collections.Shuffle only shuffling once
然而,它似乎做了一次洗牌,将它添加到数组列表中(像是应该),但接下来的49次,它不洗牌。它只添加相同的一个。 您可以了解更多从下面我的代码:
int chromeSize;
ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
ArrayList<Integer> addToFirstChrome = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> populationShuffle = new ArrayList<ArrayList<Integer>>();
for (int i=0; i<geoPoints.size(); i++) {
addToFirstChrome.add(i);
}
System.out.println("add To First Chrome " + addToFirstChrome);
for (int j =0; j<50; j++) {
Collections.shuffle(addToFirstChrome);
populationShuffle.add(addToFirstChrome);
}
for (int p=0;p<populationShuffle.size();p++) {
System.out.println("Pop " + p +"=" + populationShuffle.get(p));
}
这里是输出的一个示例:
10-02 10:10:26.785: I/System.out(19648): add To First Chrome [0, 1, 2, 3, 4]
10-02 10:10:26.790: I/System.out(19648): Pop 0=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 1=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 2=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 3=[2, 1, 3, 4, 0]
10-02 10:10:26.790: I/System.out(19648): Pop 4=[2, 1, 3, 4, 0]
所以你看,它打乱了第一个,但现在不是了。 我在这里错过了什么吗?
非常感谢你。我可以看到我现在出了什么地方。 –