java混_Java中的混排数组

我正在搞一些示例代码,默认情况下,它将向下粘贴的代码顶部显示一个字符串数组。我想随机化数组的显示方式。我试着直接在声明数组的地方添加Collections.shuffle(messages);,但它不起作用。我使用了正确的导入,所以这不是问题。我对如何随机化阵列没有很好的把握,但这是我从我所做的研究中得出的最佳尝试。谁能帮忙?Java中的混排数组

class Producer

implements Runnable

{

private BlockingQueue drop;

List messages = Arrays.asList(

"Mares eat oats",

"Does eat oats",

"Little lambs eat ivy",

"Wouldn't you eat ivy too?");

public Producer(BlockingQueue 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 drop;

public Consumer(BlockingQueue 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 drop = new SynchronousQueue();

(new Thread(new Producer(drop))).start();

(new Thread(new Consumer(drop))).start();

}

}

2014-10-28

Brett

+1

在你的构造函数中调用它? –

2014-10-28 16:54:33

+1

[随机洗牌数组]可能重复(http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array) –

2014-10-28 16:56:36

+0

我实际上确实在我的构造函数中调用它,显然在错误的位置。在我的帖子忘了提及。 –

2014-10-28 17:00:46

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值