Collections. shuffle () Method Instance parsing,

Source: Internet
Author: User
Tags shuffle

Collections. shuffle () Method Instance parsing,

This article focuses on the related content of the Collections. shuffle () method. Let's take a look at the specific content.

Java. util. Collections class has a static shuffle () method, as follows:

1) static void shuffle (List <?> List) use the default random source to replace the list. The probability of all replications is roughly equal.

2) static void shuffle (List <?> List, Random rand) Replace the specified list with the specified Random source. All replications are generally equal. It is assumed that the Random source is fair.

In other words, like shuffling, the original order is randomly disrupted.

NOTE: If an integer array is given, 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 shuffle () to disrupt the order of the underlying array.

2) Use List <Integer> list = Arrays. aslist (ia), and then use shuffle () to disrupt 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 result is 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, Arrays. the asList () output is passed to the ArrayList () constructor, which creates an ArrayList that references the ia element. Therefore, if these references are disrupted, the array is not modified. However, if you directly use the results of Arrays. asList (ia), this will change the ia sequence. It is important to realize that the List object generated by Arrays. asList () uses the underlying array as its physical implementation. As long as your operation will modify the List and you do not want the original array to be modified, you should create a copy in another container.

Summary

The above is all the content of Collections. shuffle () Method Instance parsing in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.