在 Java 中打乱或随机化列表

原文:https://www . geesforgeks . org/shuffle-or-随机化-a-list-in-java/

java
    util
       Collections

语法:

Collections.shuffle(list);

示例:

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to demonstrate working of shuffle()
import java.util.*;

public class GFG {
    public static void main(String[] args)
    {
        ArrayList<String> mylist = new ArrayList<String>();
        mylist.add("ide");
        mylist.add("quiz");
        mylist.add("geeksforgeeks");
        mylist.add("quiz");
        mylist.add("practice");
        mylist.add("qa");

        System.out.println("Original List : \n" + mylist);

        Collections.shuffle(mylist);

        System.out.println("\nShuffled List : \n" + mylist);
    }
}

Output: 

Original List : 
[ide, quiz, geeksforgeeks, quiz, practice, qa]

Shuffled List : 
[ide, practice, quiz, qa, geeksforgeeks, quiz]