Java中的 Collections.shuffle() 方法和示例
正如类名所暗示的,Collections 类的 shuffle() 方法存在于名为Java.util的实用程序包中,它对列表中的元素进行混洗。
我们可以使用以下两种方式在我们的程序中实现:
- 使用预定义的随机源
- 使用用户提供的随机源
方式 1:使用预定义的随机源对给定列表进行洗牌。
句法:
public static void shuffle(List mylist)
抛出异常:如果给定列表或其列表迭代器不支持集合操作,则抛出UnsupportedOperationException 。
例子:
Java
// Java program to demonstrate
// working of shuffle() method
// of Collections class
// Importing utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty ArrayList of string type
ArrayList mylist = new ArrayList();
// Adding custom input elements to list object
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
mylist.add("quiz");
mylist.add("practice");
mylist.add("qa");
// Printing list before shuffling
System.out.println("Original List : \n" + mylist);
// Shuffling the list
Collections.shuffle(mylist);
// Printing list after shuffling
System.out.println("\nShuffled List : \n" + mylist);
}
}
Java
// Java Program to demonstrate working of shuffle()
// with user provided source of randomness
// Importing required utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty ArrayList of string type
ArrayList mylist = new ArrayList();
// Adding custom input elements to above created
// object
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
mylist.add("quiz");
mylist.add("practice");
mylist.add("qa");
// Print and display the elements of List on console
System.out.println("Original List : \n" + mylist);
// Shuffling the given list
// using Random() method
Collections.shuffle(mylist, new Random());
// Print the updated list on console
System.out.println(
"\nShuffled List with Random() : \n" + mylist);
// Shuffling list by using Random(3)
Collections.shuffle(mylist, new Random(3));
// Print the updated list on console
System.out.println(
"\nShuffled List with Random(3) : \n" + mylist);
// Again shuffling list by using Random(3)
Collections.shuffle(mylist, new Random(5));
System.out.println(
"\nShuffled List with Random(5) : \n" + mylist);
}
}
Original List :
Shuffled List :
方式 2:使用用户提供的随机源对给定列表进行洗牌。
这里提供了一个附加参数,上面指定的“ rndm”是随机化列表的来源。
句法:
public static void shuffle(List mylist, Random rndm)
参数:这里它需要列出的两个参数
- 列表
- 随机性的来源
例外:如果指定的列表或其列表迭代器不支持集合操作,则出现UnsupportedOperationException 。
例子:
Java
// Java Program to demonstrate working of shuffle()
// with user provided source of randomness
// Importing required utility classes
import java.util.*;
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an empty ArrayList of string type
ArrayList mylist = new ArrayList();
// Adding custom input elements to above created
// object
mylist.add("code");
mylist.add("quiz");
mylist.add("geeksforgeeks");
mylist.add("quiz");
mylist.add("practice");
mylist.add("qa");
// Print and display the elements of List on console
System.out.println("Original List : \n" + mylist);
// Shuffling the given list
// using Random() method
Collections.shuffle(mylist, new Random());
// Print the updated list on console
System.out.println(
"\nShuffled List with Random() : \n" + mylist);
// Shuffling list by using Random(3)
Collections.shuffle(mylist, new Random(3));
// Print the updated list on console
System.out.println(
"\nShuffled List with Random(3) : \n" + mylist);
// Again shuffling list by using Random(3)
Collections.shuffle(mylist, new Random(5));
System.out.println(
"\nShuffled List with Random(5) : \n" + mylist);
}
}
Original List :
Shuffled List with Random() :
[quiz, practice, quiz, geeksforgeeks, qa, code]
Shuffled List with Random(3) :
Shuffled List with Random(5) :
[geeksforgeeks, qa, quiz, code, practice, quiz]
但在实施此方法之前,请记住下面列出的某些要点 如所列 以下 作为 如下:
- 内部工作:此方法随机排列列表中的元素。
- 运行时:它以线性时间运行。
- 访问元素:
- 它从最后一个元素到第二个元素向后遍历列表,反复将随机选择的元素交换到其“当前位置”。
- 此后,从列表中从第一个元素到当前位置(含)的部分中随机选择元素。
Note: If the provided list does not implement the RandomAccess interface, like LinkedList, and is large, it first copies the list into an array, then shuffles the array copy, and finally copies the array back into the list. This makes sure that the time remains linear.