當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Collections.shuffle()用法及代碼示例


java.util.Collections.shuffle()是java.util.Collections類方法。

// Shuffles mylist
public static void shuffle(List mylist)
This method throws UnsupportedOperationException if the 
given list or its list-iterator does not support 
the set operation.
// Java program to demonstrate working of shuffle() 
import java.util.*; 
  
public class GFG 
{ 
    public static void main(String[] args) 
    { 
        ArrayList<String>  mylist = new ArrayList<String>(); 
        mylist.add("code"); 
        mylist.add("quiz"); 
        mylist.add("geeksforgeeks"); 
        mylist.add("quiz"); 
        mylist.add("practice"); 
        mylist.add("qa"); 
  
        System.out.println("Original List : \n" + mylist); 
  
        Collections.shuffle(mylist); 
  
        System.out.println("\nShuffled List : \n" + mylist); 
    } 
}

輸出:

 
Original List : 
[code, quiz, geeksforgeeks, quiz, practice, qa]

Shuffled List : 
[qa, quiz, practice, code, quiz, geeksforgeeks]


shuffle(mylist, rndm)

它使用用戶提供的隨機源對給定列表進行混洗。

// mylist is the list to be shuffled.
// rndm is source of randomness to shuffle the list.
public static void shuffle(List mylist, Random rndm)

It throws  UnsupportedOperationException if the specified list or
its list-iterator does not support the set operation.
// Java program to demonstrate working of shuffle() 
// with user provided source of randomness. 
import java.util.*; 
  
public class GFG 
{ 
    public static void main(String[] args) 
    { 
        ArrayList<String>  mylist = new ArrayList<String>(); 
        mylist.add("code"); 
        mylist.add("quiz"); 
        mylist.add("geeksforgeeks"); 
        mylist.add("quiz"); 
        mylist.add("practice"); 
        mylist.add("qa"); 
  
        System.out.println("Original List : \n" + mylist); 
  
        // Here we use Random() to shuffle given list. 
        Collections.shuffle(mylist, new Random()); 
        System.out.println("\nShuffled List with Random() : \n"
                                                     + mylist); 
  
        // Here we use Random(3) to shuffle given list. 
        Collections.shuffle(mylist, new Random(3)); 
        System.out.println("\nShuffled List with Random(3) : \n"
                                                     + mylist); 
  
        // Here we use Random(3) to shuffle given list. 
        Collections.shuffle(mylist, new Random(5)); 
        System.out.println("\nShuffled List with Random(5) : \n"
                                                     + mylist); 
  
    } 
}

輸出:

Original List : 
[code, quiz, geeksforgeeks, quiz, practice, qa]

Shuffled List with Random() : 
[qa, quiz, geeksforgeeks, code, practice, quiz]

Shuffled List with Random(3) : 
[quiz, code, quiz, practice, qa, geeksforgeeks]

Shuffled List with Random(5) : 
[code, practice, geeksforgeeks, qa, quiz, quiz]

重要事項:

  • 此方法通過隨機排列列表元素來工作
  • 以線性時間運行。如果提供的列表沒有實現LinkedList之類的RandomAccess接口,並且該列表很大,它將首先將列表複製到數組中,然後將數組副本混洗,最後將數組複製回列表中。這樣可以確保時間保持線性。
  • 它從最後一個元素到第二個元素向後遍曆列表,反複將隨機選擇的元素交換到“current position”中。從列表中從第一個元素到當前位置(包括首尾)的部分中隨機選擇元素。 [來源:javadoc]

參考:
https://developer.microej.com/javadoc/microej_4.0/addons/java/util/Collections.html#shuffle-java.util.List-



相關用法


注:本文由純淨天空篩選整理自 Collections.shuffle() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。