【发布时间】:2016-01-13 22:06:39
【问题描述】:
所以我有以下数组:
String [] randomList = new String [16];
randomList[0]="Dog";
randomList[1]="Dog";
randomList[2]="Cat";
randomList[3]="Cat";
randomList[4]="Mouse";
randomList[5]="Mouse";
randomList[6]="Car";
randomList[7]="Car";
randomList[8]="Phone";
randomList[9]="Phone";
randomList[10]="Game";
randomList[11]="Game";
randomList[12]="Computer";
randomList[13]="Computer";
randomList[14]="Toy";
randomList[15]="Toy";
我只想打乱这个数组的前 9 个元素。我使用了以下代码,但它会打乱整个数组。
Collections.shuffle(Arrays.asList(randomList));
我将如何只洗牌数组的一部分而不是整个东西?我正在制作一个非常简单的程序,所以我想继续使用 Collections 类,但是欢迎所有解决方案。谢谢
【问题讨论】:
-
你也应该用
String [] randomList = {"Dog,"Dog","Cat","Cat","Mouse" /*...*/};初始化你的数组。