Java Collections.shuffle方法

shuffle()是一个Java Collections类方法,其工作原理是随机置换指定列表元素。有两种不同类型的Java shuffle()方法,可以根据其参数进行区分。这些都是:

  1. Java Collections shuffle(list)方法
  2. Java Collections shuffle (list, random) 方法

Java Collections shuffle(list)方法

shuffle(list)方法用于通过使用默认随机性对指定的列表元素进行随机重新排序来工作。

Java Collections shuffle(列表,随机)方法

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

句法

以下是shuffle()方法的声明:

 
  1. public static void shuffle(List<?> list)  
  2. public static void shuffle(List<?> list, Random random)   

参数

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

返回值

洗牌()方法不返回任何东西。

例外情况

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

相容版本

Java 1.5及更高版本

例子1

 
  1. import java.util.*;  
  2. public class CollectionsShuffleExample1 {  
  3.     public static void main(String[] args) {  
  4.         List<String> list = Arrays.asList("A", "B", "C", "D");  
  5.             System.out.println("List before Shuffle : "+list);  
  6.             Collections.shuffle(list);  
  7.             System.out.println("List after shuffle : "+list);  
  8.           }  
  9. }  
  10.  

输出:

List before Shuffle : [A, B, C, D]
List after shuffle : [A, C, D, B]

例子2

 
  1. import java.util.*;  
  2. public class CollectionsShuffleExample2 {  
  3.     public static void main(String[] args) {  
  4.         //Create linked list object  
  5.           LinkedList<Integer> list = new LinkedList<Integer>();  
  6.           //Add values in the list  
  7.           list.add(10);  
  8.           list.add(-20);  
  9.           list.add(50);  
  10.           list.add(90);  
  11.           list.add(-15);  
  12.             System.out.println("List before Shuffle : "+list);  
  13.             Collections.shuffle(list);  
  14.             System.out.println("List after shuffle : "+list);  
  15.           }  
  16. }

输出:

List before Shuffle : [10, -20, 50, 90, -15]
List after shuffle : [10, 50, 90, -15, -20]

例子3

 
  1. import java.util.*;  
  2. public class CollectionsShuffleExample3 {  
  3.     public static void main(String[] args) {  
  4.         //Create linked list object  
  5.           LinkedList<Integer> list = new LinkedList<Integer>();  
  6.           //Add values in the list  
  7.           list.add(45);  
  8.           list.add(20);  
  9.           list.add(55);  
  10.           list.add(90);  
  11.           list.add(15);  
  12.           System.out.println("List before Shuffle = "+list);  
  13.           //We use Random() to shuffle given list.  
  14.           Collections.shuffle(list, new Random());  
  15.           System.out.println("Shuffled List with Random() = "+list);       
  16.           //We use Random(3) to shuffle given list.  
  17.           Collections.shuffle(list, new Random(3));  
  18.           System.out.println("Shuffled List with Random(3) = "+list);            
  19.           }  
  20. }  
  21.   

输出:

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

 
  1. import java.util.*;  
  2. public class CollectionsShuffleExample4 {  
  3.     public static void main(String[] args) {          
  4.         List<String> list = Arrays.asList("one", "two", "three", "four");  
  5.             System.out.println(list);  
  6.             Collections.shuffle(list, new Random(2));  
  7.             System.out.println(list);         
  8.           }  
  9. }   

输出:

[one, two, three, four]
[four, two, one, three]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值