就是随机打乱原来的顺序,和洗牌一样。如:
// ShuffleTest.java
import java.util.*;
public class ShuffleTest {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
list.add(new Integer(i));
System.out.println("打乱前:");
System.out.println(list);
for (int i = 0; i < 5; i++) {
System.out.println("第" + i + "次打乱:");
Collections.shuffle(list);
System.out.println(list);
}
}
}
输出结果:
打乱前:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
第0次打乱:
[6, 3, 2, 0, 8, 1, 7, 5, 4, 9]
第1次打乱:
[6, 2, 3, 0, 8, 5, 7, 4, 9, 1]
第2次打乱:
[1, 7, 9, 4, 6, 0, 2, 5, 3, 8]
第3次打乱:
[0, 4, 2, 8, 9, 1, 3, 7, 5, 6]
第4次打乱:
[8, 1, 3, 0, 7, 9, 4, 2, 5, 6]
摘自:http://topic.csdn.net/u/20080308/17/642f73e3-ff7b-4144-a52b-190a72be1e49.html