希望标题解释我想要的。我需要一组随机顺序的数字1-12,但不会得到空结果。生成没有null的随机数字1-12?
这里是我的代码:
Integer[] arr = new Integer[12];
for(int i = 1; i < arr.length; i++)
{
arr[i] = i+1; //With this not being i+1, I get 0-11. Like this, I get null-12 and no 1.
if (arr[i] == null)
{
arr[i] = 1;
}
}
Collections.shuffle(Arrays.asList(arr));
System.out.println(Arrays.asList(arr));
这里是我所得到的输出:
[null, 5, 3, 10, 7, 2, 9, 4, 6, 12, 8, 11]
我如何转换空,因为我的if语句什么也不做?
数组下标从0开始!而你从来没有设置元素0 ... – fge