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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值