The main research in this article is the relevant content of the Collections.shuffle() method. Let’s take a look at the specific content below.
There is a static shuffle() method under the Java.util.Collections class, as follows:
1) static void shuffle(List<?> list) Use the default random source to pervert the list, and the possibility of all permutations occur is roughly equal.
2) static void shuffle(List<?> list, Random rand) Performs the specified list using the specified random source. The possibility of all permutations occur is roughly equal, assuming that the random source is fair.
To put it simply, it is like a shuffle, randomly disrupting the original order.
Note: If you are given an integer array, use the Arrays.asList() method to convert it into a collection class, there are two ways:
1) Use List<Integer> list=ArrayList(Arrays.asList(ia)), and use shuffle() to disrupt it will not change the order of the underlying array.
2) Use List<Integer> list=Arrays.aslist(ia), and then use shuffle() to disrupt it will change the order of the underlying array. The code example is as follows:
package ahu;import java.util.*;public class Modify {public static void main(String[] args){Random rand=new Random(47);Integer[] ia={0,1,2,3,4,5,6,7,8,9};List<Integer> list=new ArrayList<Integer>(Arrays.asList(ia));System.out.println("Before shufflig: "+list);Collections.shuffle(list,rand);System.out.println("After shuffling: "+list);System.out.println("array: "+Arrays.toString(ia));List<Integer> list1=Arrays.asList(ia);System.out.println("Before shuffling: "+list1);Collections.shuffle(list1,rand);System.out.println("After shuffling: "+list1);System.out.println("array: "+Arrays.toString(ia));}}The operation results are as follows:
3, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] After shuffling: [8, 0, 5, 2, 6, 1, 4, 9, 3, 7] After shuffling: [8, 0, 5, 2, 6, 1, 4, 9, 3, 7]
In the first case, the output of Arrays.asList() is passed to the constructor of ArrayList(), which creates an ArrayList that references the element of ia, so disrupting these references does not modify the array. However, if the result of Arrays.asList(ia) is used directly, this disruption will modify the order of ia. It is important to realize that the List object produced by Arrays.asList() will use the underlying array as its physical implementation. As long as the action you perform will modify this List and you don't want the original array to be modified, you should create a copy in another container.
Summarize
The above is all the content of this article about the analysis of the Collections.shuffle() method example, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!