Java阵列协助

时间:2015-04-23 23:08:42

标签: java eclipse

我有以下2个数组代码。

?

我有两个问题。

nums [6]的价值是什么?

nums [max]的值是多少?

3 个答案:

答案 0 :(得分:1)

来自Java语言规范:

 Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10)
 For type int, the default value is zero, that is, 0.

所以第一个问题的答案是0。

在Java中,数组索引从0开始。因此array包含array.length个元素,索引为0, 1, 2, ... ,array.length - 1,而array[array.length]会抛出ArrayOutOfBoundsException

答案 1 :(得分:0)

nums [6]将为0,因为int数组将保留默认值。

nums [max]将抛出异常,因为数组的长度等于max。

答案 2 :(得分:0)

我希望这能回答你的问题。 nuns [6]为0,因为它是java给那些尚未指定的数组的默认值。