Alex Rivera | Logout

How can I shuffle a specific range of an ArrayList?

Asked 2011-08-16T14:28:51.203
16

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)
Edit
Report

1 Answer

0

It's simple

public void shuffleArrayListInTheRange(int start, int end) {
    Collections.shuffle(list.subList(start, end));
}
answered 2011-08-16T14:33:34.073

Your Answer