【发布时间】:2013-02-18 18:15:24
【问题描述】:
以下代码示例是创建两个随机值,还是重复使用第一个?
Random r = new Random();
int[] a = new int[10];
a[r.nextInt(10)] += 1;
// Equals this, creating two random values:
a[r.nextInt(10)] = a[r.nextInt(10)] + 1;
// Or this, using 6 as the result of the first random operation
a[6] = 6 + 1;
编辑:当我们这样做的时候,这个操作符(以及其他像-=、/= 等)有名字吗?
【问题讨论】:
-
虽然我没有投反对票,但了解
+=是什么就足以回答这个问题,这在 JLS 中。测试很容易,尽管您需要进行迭代以确保您超过了重复随机值的概率。您不需要长时间迭代。
标签: java random assignment-operator