Java Collections shuffle()方法和示例

集合类shuffle()方法 (Collections Class shuffle() method)

Syntax:

句法:

    public static void shuffle(List l);
    public static void shuffle(List l, Random ran);

  • shuffle() method is available in java.util package.

    shuffle()方法在java.util包中可用。

  • shuffle(List l) method is used to shuffle elements of the given list randomly by default.

    shuffle(List l)方法用于默认情况下随机地随机洗净给定列表的元素。

  • shuffle(List l, Random ran) method is used to shuffle elements of the given list by using the given Random (ran).

    shuffle(List l,Random ran)方法用于通过使用给定的Random(随机)对给定列表的元素进行随机播放。

  • These methods may throw an exception at the time of shuffling elements of the list.

    这些方法在对列表元素进行混排时可能会引发异常。

    UnsupportedOperationException: This exception may throw when the given parameter List (l) or its iterator un-support set operation.

    UnsupportedOperationException :当给定参数List(l)或其迭代器不支持set操作时,可能引发此异常。

  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

    这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则也不会出现任何错误。

Parameter(s):

参数:

  • In the first case, shuffle(List l),

    在第一种情况下, shuffle(List l)

    • List l – represents the list that is for shuffling.
    • 列表l –表示要改组的列表。
  • In the first case, shuffle(List l, Random ran),

    在第一种情况下, shuffle(List l,Random ran)

    • List l – represents the list that is for shuffling.
    • 列表l –表示要改组的列表。
    • Random ran – represents the direction of randomness of the given list.
    • 随机运行 –表示给定列表的随机方向。

Return value:

返回值:

In both the cases, the return type of the method is void, it does not return anything.

在这两种情况下,方法的返回类型均为void ,它不返回任何内容。

Example:

例:

// Java program to demonstrate the example 
// of shuffle() method of Collections

import java.util.*;

public class ShuffleOfCollections {
     
    public static void main(String args[]) {
     
        // Instantiates an array list object
        List < Integer > arr_l = new ArrayList < Integer > ();
        Random ran = new Random();

        // By using add() method is to add
        // objects in an array list
        arr_l.add(20);
        arr_l.add(10);
        arr_l.add(40);
        arr_l.add(30);
        arr_l.add(50);

        // Display ArrayList
        System.out.println("ArrayList: " + arr_l);

        // By using shuffle(arr_l) method is to shuffle
        // the elements of arr_l bydefault
        Collections.shuffle(arr_l);

        //Display Shuffle ArrayList
        System.out.println("Collections.shuffle(arr_l): " + arr_l);

        // By using shuffle(arr_l,ran) method is to shuffle
        // the elements of arr_l based on the defined randomness
        Collections.shuffle(arr_l, ran);


        // Display Shuffle ArrayList
        System.out.println("Collections.shuffle(arr_l,ran) :" + arr_l);
    }
}

Output

输出量

ArrayList: [20, 10, 40, 30, 50]
Collections.shuffle(arr_l): [40, 50, 10, 30, 20]
Collections.shuffle(arr_l,ran) :[20, 30, 50, 10, 40]


翻译自: https://www.includehelp.com/java/collections-shuffle-method-with-example.aspx

你可能感兴趣的