博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Collections shuffle()方法和示例
阅读量:2549 次
发布时间:2019-05-11

本文共 2845 字,大约阅读时间需要 9 分钟。

集合类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 Collectionsimport 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]

翻译自:

转载地址:http://osvzd.baihongyu.com/

你可能感兴趣的文章
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>
Blob(二进制)、byte[]、long、date之间的类型转换
查看>>
linux awk命令详解
查看>>
OO第一次总结博客
查看>>
day7
查看>>
iphone移动端踩坑
查看>>
vs无法加载项目
查看>>
Beanutils基本用法
查看>>
玉伯的一道课后题题解(关于 IEEE 754 双精度浮点型精度损失)
查看>>
《BI那点儿事》数据流转换——百分比抽样、行抽样
查看>>
哈希(1) hash的基本知识回顾
查看>>
Leetcode 6——ZigZag Conversion
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
【转】轻松记住大端小端的含义(附对大端和小端的解释)
查看>>
【转】在Ubuntu 12.04 上为Virtualbox 启用USB 设备支持--不错
查看>>
设计模式那点事读书笔记(3)----建造者模式
查看>>
tar命令
查看>>
cnblog之初来乍到
查看>>
在win10中解决 你要以何方式打开此 .xlsx
查看>>