How to call collections on a part of a Java array Shuffle

So I have the following array:

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";

I want to change only the first nine elements of this array I used the following code, but it shuffles the entire array

Collections.shuffle(Arrays.asList(randomList));

How can I change only part of the array instead of the whole thing? I'm making a very simple program, so I want to continue using the collections class, but all solutions are welcome thank you

Solution

You can use list type's sublist method to get the list object, which contains the view of the specific element range in the original list I haven't tested this, but I think it should work:

Collections.shuffle(Arrays.asList(randomList).subList(startIndex,endIndex));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>