Java中的Shuffling数组

时间:2014-10-28 16:51:09

标签: java arrays shuffle

我正在搞乱一些示例代码,默认情况下,打印一个字符串数组,显示在我粘贴在下面的代码的顶部。我想随机化数组的显示方式。我尝试在声明数组的正下方添加Collections.shuffle(messages);,但它不起作用。我使用了正确的导入,因此这不是问题。我对如何随机化数组没有很好的把握,但这是我从研究中得出的最好的尝试。有人可以帮忙吗?

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();
    }
}

2 个答案:

答案 0 :(得分: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!");
    }
}  

答案 1 :(得分:0)

user1071777是正确的,只需在Producer的构造函数中调用shuffle。

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();
    }
}