Collections.shuffle of method instance resolution

  • 2021-01-06 00:33:45
  • OfStack

This paper mainly studies the relevant content of the Collections.shuffle () method. Let's take a look at the specific content.

Java.util.Collections There is a static shuffle() method under the Java.util.Collections class, as follows:

1) static void shuffle(List < ? > list) transposes the list using the default random source, and all transposes are approximately equally likely to occur.

2) static void shuffle(List < ? > list, Random rand) permutations the specified list with the specified random source. All permutations are approximately equally likely to occur, assuming that the random source is fair.

Commonly speaking, it's like shuffling a deck of cards, randomly shuffling the original order.

Note: If you are given an integer array, there are two ways to convert it to a collection class using the Arrays.asList () method:

1) use List < Integer > list=ArrayList(Arrays.asList (ia)), using shuffle() does not change the order of the underlying array.

2) use List < Integer > list= Arrays.aslist (ia), then shuffling with shuffle() 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 running results are as follows:


Before shufflig: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
After shuffling: [3, 5, 2, 0, 7, 6, 1, 4, 9, 8] 
array: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
Before shuffling: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
After shuffling: [8, 0, 5, 2, 6, 1, 4, 9, 3, 7] 
array: [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 elements of ia, so shuffling these references does not modify the array. However, if the results of Arrays.asList(ia) are used directly, this shuffling will modify the order of ia. It is important to realize that the List objects produced by Arrays.asList () use the underlying array as their physical implementation. As long as you perform an operation that changes the List, and you don't want the original array to be changed, you should create a copy in another container.

conclusion

Collections.shuffle() method example analysis is all about this article, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: