Question:
Using Collections.shuffle() to shuffle an array.
Code:
import java.util.*; public class ShuffleCards { public static void main(String[] args) { Integer[] cards = new Integer[10]; // Initialize the array to the ints 0-51 for (int i=0; i < cards.length; i++) { cards[i] = i; } // print out contents of array System.out.println(Arrays.toString(cards)); // Shuffle using Collections.shuffle() Collections.shuffle(Arrays.asList(cards)); // print out contents of array System.out.println(Arrays.toString(cards)); } }
Output:
$ java ShuffleCards [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [3, 9, 1, 2, 6, 8, 4, 0, 5, 7]