In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the collectionsshuffle method in java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
There is a static shuffle () method under the Java.util.Collections class, and 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 you use the results of Arrays.asList (ia) directly, this disruption will change the order of the ia. It is important to realize that the List object generated by Arrays.asList () uses the underlying array as its physical implementation. As long as you perform an operation that modifies the List, and you don't want the original array to be modified, you should create a copy in another container.
Collectionsshuffle method
1) staticvoidshuffle (Listlist) replaces the list with the default random source, and all replacements are roughly equal.
2) staticvoidshuffle (Listlist,Randomrand) replaces the specified list with the specified random source, and all permutations are roughly equal, assuming that the random source is fair.
To put it more colloquially, it is like shuffling cards, randomly disrupting the original order.
Note: given an integer array, there are two ways to convert it to a collection class with the Arrays.asList () method:
1) scrambling with Listlist=ArrayList (Arrays.asList (ia)) and shuffle () does not change the order of the underlying arrays.
2) using Listlist=Arrays.aslist (ia) and then scrambling with shuffle () will change the order of the underlying arrays.
Collectionsshuffle code example
Packageshit
Importjava.util.*
PublicclassModify {
Publicstaticvoidmain (String [] args) {
Randomrand=newRandom (47)
Integer [] ia= {0,1,2,3,4,5,6,7,8,9}
Listlist=newArrayList (Arrays.asList (ia))
System.out.println ("Beforeshufflig:" + list)
Thank you for reading this article carefully. I hope the article "how to use the collectionsshuffle method in java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.