随机打乱一个数组(比如斗地主发牌会用上!)
Run.java
importjava.util.Random;
publicclass Run {
Random random = new Random();
publicvoid test() {
String a = "红A,红2,红3,红4,红5,红6,红7,红8,红9,红10,红J,红Q,红K,桃A,桃2,桃3,桃4,桃5,桃6,桃7,桃8,桃9,桃10,桃J,桃Q,桃K, 杏A,杏2,杏3,杏4,杏5,杏6,杏7,杏8,杏9,杏10,杏J,杏Q,杏K,方A,方2,方3,方4,方5,方6,方7,方8,方9,方10,方J,方Q,方K,小王,大王";
String A[] =a.split(",");
String B[] = new String[A.length];
for (int i = 0; i < B.length; i++) {
int index = random.nextInt(A.length);
if (A[index] != null) {
B[i] = A[index];
A[index] = null;
} else {
i--;
continue;
}
System.out.print(B[i] + " ");
}
}
publicstaticvoid main(String[] args) {
Run d = new Run();
d.test();
}
}