2014-10-28 23 views
0

我正在搞一些示例代碼,默認情況下,它將向下粘貼的代碼頂部顯示一個字符串數組。我想隨機化數組的顯示方式。我試着直接在聲明數組的地方添加Collections.shuffle(messages);,但它不起作用。我使用了正確的導入,所以這不是問題。我對如何隨機化陣列沒有很好的把握,但這是我從我所做的研究中得出的最佳嘗試。誰能幫忙?Java中的混排數組

class Producer 
    implements Runnable 
{ 
    private BlockingQueue<String> drop; 
    List<String> messages = Arrays.asList(
     "Mares eat oats", 
     "Does eat oats", 
     "Little lambs eat ivy", 
     "Wouldn't you eat ivy too?"); 


    public Producer(BlockingQueue<String> d) { this.drop = d; } 

    public void run() 
    { 
     try 
     { 
      for (String s : messages) 
       drop.put(s); 
      drop.put("DONE"); 
     } 
     catch (InterruptedException intEx) 
     { 
      System.out.println("Interrupted! " + 
       "Last one out, turn out the lights!"); 
     } 
    }  
} 

class Consumer 
    implements Runnable 
{ 
    private BlockingQueue<String> drop; 
    public Consumer(BlockingQueue<String> d) { this.drop = d; } 

    public void run() 
    { 
     try 
     { 
      String msg = null; 
      while (!((msg = drop.take()).equals("DONE"))) 
       System.out.println(msg); 
     } 
     catch (InterruptedException intEx) 
     { 
      System.out.println("Interrupted! " + 
       "Last one out, turn out the lights!"); 
     } 
    } 
} 

public class SynQApp 
{ 
    public static void main(String[] args) 
    { 
     BlockingQueue<String> drop = new SynchronousQueue<String>(); 
     (new Thread(new Producer(drop))).start(); 
     (new Thread(new Consumer(drop))).start(); 
    } 
} 
+1

在你的構造函數中調用它? – user1071777 2014-10-28 16:54:33

+1

[隨機洗牌數組]可能重複(http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) – Chiseled 2014-10-28 16:56:36

+0

我實際上確實在我的構造函數中調用它,顯然在錯誤的位置。在我的帖子忘了提及。 – Brett 2014-10-28 17:00:46

回答

2

這應該工作:

public void run() 
{ 
    Collections.shuffle(messages);//randomize array before adding into the queue 

    try 
    { 
     for (String s : messages) 
      drop.put(s); 
     drop.put("DONE"); 
    } 
    catch (InterruptedException intEx) 
    { 
     System.out.println("Interrupted! " + 
      "Last one out, turn out the lights!"); 
    } 
} 
0

user1071777是正確的,只是調用生產者的構造洗牌。

class Producer 
    implements Runnable 
{ 
    private BlockingQueue<String> drop; 
    List<String> messages = Arrays.asList(
     "Mares eat oats", 
     "Does eat oats", 
     "Little lambs eat ivy", 
     "Wouldn't you eat ivy too?"); 


    public Producer(BlockingQueue<String> d) 
    { 
     this.drop = d; 
     Collections.shuffle(messages); 
    } 

    public void run() 
    { 
     try 
     { 
      for (String s : messages) 
       drop.put(s); 
      drop.put("DONE"); 
     } 
     catch (InterruptedException intEx) 
     { 
      System.out.println("Interrupted! " + 
       "Last one out, turn out the lights!"); 
     } 
    }  
} 

class Consumer 
    implements Runnable 
{ 
    private BlockingQueue<String> drop; 
    public Consumer(BlockingQueue<String> d) { this.drop = d; } 

    public void run() 
    { 
     try 
     { 
      String msg = null; 
      while (!((msg = drop.take()).equals("DONE"))) 
       System.out.println(msg); 
     } 
     catch (InterruptedException intEx) 
     { 
      System.out.println("Interrupted! " + 
       "Last one out, turn out the lights!"); 
     } 
    } 
} 

public class SynQApp 
{ 
    public static void main(String[] args) 
    { 
     BlockingQueue<String> drop = new SynchronousQueue<String>(); 
     (new Thread(new Producer(drop))).start(); 
     (new Thread(new Consumer(drop))).start(); 
    } 
}