import java.util.*; public class ShuffleExample { public static void main(String[] args) { Listnames = new ArrayList<>(); names.add("John"); names.add("Mary"); names.add("Mike"); names.add("Jane"); System.out.println(names); Collections.shuffle(names); System.out.println(names); } }
[John, Mary, Mike, Jane] [Mike, Jane, John, Mary]
import java.util.*; public class DeckOfCards { public static void main(String[] args) { Listdeck = new ArrayList<>(); String[] suits = {"Hearts", "Diamonds", "Clubs", "Spades"}; String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; for (String suit : suits) { for (String rank : ranks) { deck.add(rank + " of " + suit); } } System.out.println(deck); Collections.shuffle(deck); System.out.println(deck); } }
[Ace of Hearts, 2 of Hearts, 3 of Hearts, ..., King of Spades] [4 of Diamonds, King of Hearts, 5 of Spades, ..., 2 of Diamonds]The shuffle() method belongs to the java.util.Collections class, which is part of the Java Standard Library.