java.util.Collections
class contains a static shuffle(list) method, but this
doesn't work with arrays, only Lists (eg, ArrayList).
Random rgen = new Random(); // Random number generator
int[] cards = new int[52];
//--- Initialize the array to the ints 0-51
for (int i=0; i<52; i++) {
cards[i] = i;
}
//--- Shuffle by exchanging each element randomly
for (int i=0; i<52; i++) {
int randomPosition = rgen.nextInt(52);
int temp = cards[i];
cards[i] = card[randomPosition];
cards[randomPosition] = temp;
}
java.util.Collections.shuffle() method.
Arrays of objects can easily be converted to ArrayLists
by using the java.util.Arrays.asList(. . .) method.