2014-10-28 22 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(); 
    } 
}