Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
行为或Java数组_Java_Arrays - Fatal编程技术网

行为或Java数组

行为或Java数组,java,arrays,Java,Arrays,我在写一段随机的代码,这让我很困扰,为什么在这种情况下输出是100000000: public class HelloWorld { public static void main(String[] args) { try { int[] array = {1}; System.out.print(array[100000000]); } catch (Exception e) { System.out.print(e.getMessage()); } }

我在写一段随机的代码,这让我很困扰,为什么在这种情况下输出是100000000:

public class HelloWorld {
    public static void main(String[] args) {
try {
    int[] array = {1};
    System.out.print(array[100000000]);
} catch (Exception e) {
    System.out.print(e.getMessage());
}
    }

}

为什么它打印100000000,我在哪里可以阅读更多关于这种行为的信息?谢谢。

您试图访问索引大于(或等于)数组长度的数组元素


ArrayIndexOutOfBoundsException#getMessage()
似乎返回一个表示您试图访问的索引的
字符串。

您试图在大于(或等于)数组长度的索引处访问数组元素


ArrayIndexOutOfBoundsException#getMessage()
似乎返回一个表示您试图访问的索引的
字符串。

因为这是抛出的异常java.lang.ArrayIndexOutOfBoundsException的message属性设置的。尝试添加e.printStackTrace();在您的catch块中,它将打印如下内容:

java.lang.ArrayIndexOutOfBoundsException:100000000
位于com.xxx.SomeTest.main(SomeTest.java:>)

,因为抛出的异常java.lang.ArrayIndexOutOfBoundsException的消息属性设置为该属性。尝试添加e.printStackTrace();在您的catch块中,它将打印如下内容:

java.lang.ArrayIndexOutOfBoundsException:100000000
在com.xxx.SomeTest.main(SomeTest.java:>)

我希望它抛出ArrayIndexOutOfBoundsException,但我忽略了一个事实,即我只是在异常中打印消息。谢谢你,这很有帮助!我原以为它会抛出ArrayIndexOutOfBoundsException,但我忽略了一个事实,即我只是在异常中打印消息。谢谢你,这很有帮助!