Ale*_*der 0 java arrays random indexoutofboundsexception
我试图使用SecureRandom对象(索引)填充数组(probCount)来确定概率.我有一个for循环运行大约1亿次:
probCount[index.nextInt(probCount.length)]++;
Run Code Online (Sandbox Code Playgroud)
当我打印数组时,我的所有元素都正确填充.这条线怎么不抛出一个越界异常?
我有它喜欢:
probCount.length - 1
Run Code Online (Sandbox Code Playgroud)
然而,这从未填充过我的最后一个元素.我有点困惑.
返回伪随机,在0(包括)和指定值(不包括)之间均匀分布的int值
...
参数:
bound
- 上限(不包括).必须是积极的.
注意"独家"部分.因此,例如,如果您调用index.nextInt(5)
它将返回0,1,2,3或4 ...这正是长度为5的数组的有效索引.