In Java, I know that to shuffle an ArrayList, the method Collections.shuffle() exists, however this shuffles the entire list.
How can I write a method (or, can someone write it and show me it?) such as the following:
private ArrayList<AnObject> list;
/**
* Shuffles the concents of the array list in the range [start, end], and
* does not do anything to the other indicies of the list.
*/
public void shuffleArrayListInTheRange(int start, int end)
Use
List.subListandCollections.shuffle, like this:(Note that the second index to
subListexclusive, so useend+1if you want to includeendindex in the shuffle.)Since
List.subListreturns a view of the list, changes made (by the shuffle method) to the sub list, will also affect the original list.