所以我有以下数组:
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类,但欢迎所有解决方案.谢谢
解决方法
您可以使用
List type’s subList method获取List对象,其中包含原始列表中特定元素范围的视图.我没有测试过这个,但我认为它应该有效:
Collections.shuffle(Arrays.asList(randomList).subList(startIndex,endindex));