Java 遍历数组的所有元素时出现 ArrayIndexOutOfBoundsException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4420815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:17:27  来源:igfitidea点击:

ArrayIndexOutOfBoundsException when iterating through all the elements of an array

javaarrays

提问by Gain

how to handle this exception "ArrayIndexOutOfBoundsException" my code : I create an array of 64 length then I intialized every index then I print the indexes to make sure I am fulling all indexes but it prints up to 63 then gives the exception !! any idea

如何处理这个异常“ArrayIndexOutOfBoundsException”我的代码:我创建一个长度为 64 的数组,然后初始化每个索引,然后打印索引以确保我已填满所有索引,但它最多打印 63 然后给出异常!任何的想法

    public static void main(String [] arg) {
    int [] a=new int [64];
    for(int i=1;i<=a.length;i++){
        a[i]=i;
        System.out.println(i);
    }

}

回答by Petar Minchev

The array indexes in Java start from 0and go to array.length - 1. So change the loop to for(int i=0;i<a.length;i++)

Java 中的数组索引从 开始0并到array.length - 1. 所以将循环更改为for(int i=0;i<a.length;i++)

回答by Caner

Indexes start from 0 so last index is 63. Change your for loop like this:
for(int i=0;i<a.length;i++){

索引从 0 开始,所以最后一个索引是 63。像这样改变你的 for 循环:
for(int i=0;i<a.length;i++){

回答by Robert

In Java arrays always start at index 0. So if you want the last index of an array to be 64, the array has to be of size 64+1 = 65.

在 Java 中,数组总是从索引 0 开始。因此,如果您希望数组的最后一个索引为 64,则数组的大小必须为 64+1 = 65。

//                       start   length
int[] myArray = new int [1    +  64    ];

回答by fastcodejava

You can correct your program this way :

您可以通过以下方式更正您的程序:

int i = 0;     // Notice it starts from 0
while (i < a.length) {
    a[i]=i;
    System.out.println(i++);
}

回答by Michael Konietzka

See the JLS-Arrays:

请参阅JLS 阵列

If an array has n components, we say n is the length of the array; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.

如果一个数组有 n 个分量,我们说 n 是数组的长度;使用从 0 到 n - 1(含)的整数索引来引用数组的组件。

So you have to iterate through [0,length()-1]

所以你必须遍历 [0,length()-1]

for(int i=0;i<a.length;i++) {
    a[i]=i+1;  //add +1, because you want the content to be 1..64
    System.out.println(a[i]);

}

回答by Michael Konietzka

You've done your math wrong. Arrays begin counting at 0. E.g. int[] d = new int[2] is an array with counts 0 and 1.

你算错了。数组从 0 开始计数。 例如 int[] d = new int[2] 是一个计数为 0 和 1 的数组。

You must set your integer 'i' to a value of 0 rather than 1 for this to work correctly. Because you start at 1, your for loop counts past the limits of the array, and gives you an ArrayIndexOutOfBoundsException.

您必须将整数“i”设置为 0 而不是 1 的值才能正常工作。因为您从 1 开始,所以您的 for 循环计数超过了数组的限制,并给您一个 ArrayIndexOutOfBoundsException。

回答by gprathour

Need Complete Explanation? Read this

需要完整的解释吗?读这个

The index of an Array always starts from 0. Therefore as you are having 64 elements in your array then their indexes will be from 0 to 63. If you want to access the 64th element then you will have to do it by a[63].

数组的索引总是从 开始0。因此,当您的数组中有 64 个元素时,它们的索引将从0 to 63. 如果要访问第 64 个元素,则必须通过a[63].

Now if we look at your code, then you have written your condition to be for(int i=1;i<=a.length;i++)here a.lengthwill return you the actual length of the array which is 64.

现在,如果我们查看您的代码,那么您已将条件写入for(int i=1;i<=a.length;i++)此处,a.length将返回数组的实际长度,即 64。

Two thingsare happening here:

这里发生了两件事

  1. As you start the index from 1 i.e. i=1therefore you are skipping the very first element of your array which will be at the 0thindex.
  2. In the last it is trying to access the a[64]element which will come out to be the 65thelement of the array. But your array contains only 64 elements. Thus you get ArrayIndexOutOfBoundsException.
  1. 当您从 1 开始索引时,即i=1因此您正在跳过将位于0th索引处的数组的第一个元素。
  2. 最后,它试图访问a[64]65th成为数组元素的元素。但是你的数组只包含 64 个元素。因此你得到ArrayIndexOutOfBoundsException.

The correct way to iterate an array with for loop would be:

使用 for 循环迭代数组的正确方法是:

for(int i=0;i < a.length;i++)

for(int i=0;i < a.length;i++)

The index starting from 0 and going to < array.length.

索引从 0 开始到< array.length.