Shuffling the Elements of a List or Array

Use Collections.shuffle() to randomly reorder the elements in a list.
// Create a list List list = new ArrayList(); // Add elements to list // Shuffle the elements in the list Collections.shuffle(list); // Create an array String[] array = new String[]{"a", "b", "c"}; // Shuffle the elements in the array Collections.shuffle(Arrays.asList(array));

Comments

11 Mar 2010 - 3:12am by oviya (not verified)

very supportive

1 Jun 2010 - 7:54pm by Guillote (not verified)

Thanks...

//I Used to shuffle an Array of integers elements

import java.util.ArrayList;
import java.util.Collections;

public class NumAleatorio {
public ArrayList Aleatorio(){
ArrayList nums = new ArrayList();
nums.add(1);
nums.add(2);
nums.add(3);
nums.add(4);
nums.add(5);

Collections.shuffle(nums);
return nums;
}
}

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.