[英]ArrayIndexOutOfBoundsException when iterating through all the elements of an array
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 !! 如何处理这个异常“ArrayIndexOutOfBoundsException”我的代码:我创建一个64长度的数组然后我初始化每个索引然后我打印索引,以确保我正在填充所有索引,但它打印到63然后给出异常! any idea
任何想法
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);
}
}
The array indexes in Java start from 0
and go to array.length - 1
. Java中的数组索引从
0
开始,然后转到array.length - 1
。 So change the loop to for(int i=0;i<a.length;i++)
所以将循环更改为
for(int i=0;i<a.length;i++)
索引从0开始,因此最后一个索引是63.更改for循环,如下所示:
for(int i=0;i<a.length;i++){
See the JLS-Arrays : 请参阅JLS-Arrays :
If an array has n components, we say n is the length of the array;
如果一个数组有n个组件,我们说n是数组的长度; the components of the array are referenced using integer indices from 0 to n - 1, inclusive.
使用从0到n-1(包括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]);
}
The index of an Array always starts from 0
. 数组的索引始终从
0
开始。 Therefore as you are having 64 elements in your array then their indexes will be from 0 to 63
. 因此,当您在数组中有64个元素时,它们的索引将从
0 to 63
。 If you want to access the 64th element then you will have to do it by a[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.length
will return you the actual length of the array which is 64. 现在,如果我们查看你的代码,那么你已经写了条件
for(int i=1;i<=a.length;i++)
这里a.length
将返回数组的实际长度64。
Two things are happening here: 这里发生了两件事 :
i=1
therefore you are skipping the very first element of your array which will be at the 0th
index. i=1
因此您将跳过数组的第一个元素,它将位于第0th
索引处。 a[64]
element which will come out to be the 65th
element of the array. a[64]
元素,它将成为数组的65th
元素。 But your array contains only 64 elements. ArrayIndexOutOfBoundsException
. ArrayIndexOutOfBoundsException
。 The correct way to iterate an array with for loop would be: 使用for循环迭代数组的正确方法是:
for(int i=0;i < a.length;i++)
The index starting from 0 and going to < array.length
. 索引从0开始并转到
< array.length
。
You've done your math wrong. 你的数学错了。 Arrays begin counting at 0. Eg 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. 必须将整数“i”设置为0而不是1才能使其正常工作。 Because you start at 1, your for loop counts past the limits of the array, and gives you an ArrayIndexOutOfBoundsException.
因为从1开始,你的for循环计数超过数组的限制,并给你一个ArrayIndexOutOfBoundsException。
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 ];
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++);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.