Java教程

Java Collections shuffle()

shuffle()是一种Java Collections 类方法,它通过随机排列指定的列表元素来工作。有 两种不同类型的Java shuffle()方法,可以根据其参数进行区分。这些是:
Java Collections shuffle(List list)方法 Java Collections shuffle(List list, Random random)方法

Java Collections shuffle(list)方法

shuffle(list)方法用于通过使用 default 随机性。

Java Collections shuffle(List list, Random random)方法

shuffle(List list, Random random)方法用于对列表进行随机重新排序元素使用 指定的随机性。

语法

以下是 shuffle()方法的声明:
public static void shuffle(List<?> list)
public static void shuffle(List<?> list, Random random)

参数

参数 说明 必需/可选
list 这是将被改组的列表。 必需
random 它是随机源,用于随机排列列表。 必需

返回

shuffle()方法不返回任何内容。

异常

UnsupportedOperationException -如果指定的列表或其列表迭代器不支持set操作,则此方法引发异常。

兼容性版本

Java 1.5及更高版本

示例1

import java.util.*;
public class CollectionsShuffleExample1 {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("A", "B", "C", "D");
            System.out.println("List before Shuffle : "+list);
            Collections.shuffle(list);
            System.out.println("List after shuffle : "+list);
          }
}
输出:
List before Shuffle : [A, B, C, D]
List after shuffle : [A, C, D, B]

示例2

import java.util.*;
public class CollectionsShuffleExample2 {
    public static void main(String[] args) {
        //Create linked list object
          LinkedList<Integer> list = new LinkedList<Integer>();
          //Add values in the list
          list.add(10);
          list.add(-20);
          list.add(50);
          list.add(90);
          list.add(-15);
            System.out.println("List before Shuffle : "+list);
            Collections.shuffle(list);
            System.out.println("List after shuffle : "+list);
          }
}
输出:
List before Shuffle : [10, -20, 50, 90, -15]
List after shuffle : [10, 50, 90, -15, -20]

示例3

import java.util.*;
public class CollectionsShuffleExample3 {
    public static void main(String[] args) {
        //Create linked list object
          LinkedList<Integer> list = new LinkedList<Integer>();
          //Add values in the list
          list.add(45);
          list.add(20);
          list.add(55);
          list.add(90);
          list.add(15);
          System.out.println("List before Shuffle = "+list);
          //We use Random() to shuffle given list.
          Collections.shuffle(list, new Random());
          System.out.println("Shuffled List with Random() = "+list);  
          //We use Random(3) to shuffle given list.
          Collections.shuffle(list, new Random(3));
          System.out.println("Shuffled List with Random(3) = "+list);           
          }
}
输出:
List before Shuffle = [45, 20, 55, 90, 15]
Shuffled List with Random() = [45, 55, 15, 90, 20]
Shuffled List with Random(3) = [90, 55, 45, 15, 20]

示例4

import java.util.*;
public class CollectionsShuffleExample4 {
    public static void main(String[] args) {      
        List<String> list = Arrays.asList("one", "two", "three", "four");
            System.out.println(list);
            Collections.shuffle(list, new Random(2));
            System.out.println(list);       
          }
}
输出:
[one, two, three, four]
[four, two, one, three]

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4