[英]I get ArrayIndexOutofBoundsException - reverse the array
I get the output for the program mentioned below. 我得到了下面提到的程序的输出。 In addition Ii also encounter an exception as: 此外,Ii还遇到例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at ReverseOrder.main(ReverseOrder.java:15)
Why does this happen? 为什么会这样?
public class ReverseOrder {
public static void main(String[] args)
{
int anArray[]={1,2,3,4,5,6};
int anotherArray[]=new int[6];
for(int i=5,j=0;i>=0;i--,j++)
{
anotherArray[j]=anArray[i];
}
//System.out.println(anotherArray.length);
//System.out.println(anotherArray[2]);
for(int j=0;j<=anotherArray.length;j++)
{
System.out.println(anotherArray[j]);
}
}
}
The problem is here: 问题出在这里:
for(int j=0;j<=anotherArray.length;j++)
{
System.out.println(anotherArray[j]);
}
you are accessing a position out of the array. 您正在访问阵列外的位置。 This happen because method length
gives you the number of elements in the array, and since the first position of an array is 0 and not 1 you should end the loop on anotherArray.length - 1
and not anotherArray.length
. 这是因为方法length
给出了数组中元素的数量,并且因为数组的第一个位置是0而不是1,所以你应该在anotherArray.length - 1
而不是anotherArray.length
上结束循环。
There are two possible solutions to this where you modify your loop to: 有两种可能的解决方案,您可以将循环修改为:
a) for(int j=0;j<=anotherArray.length - 1;j++)
or a) for(int j=0;j<=anotherArray.length - 1;j++)
或
b) for(int j=0;j<anotherArray.length;j++)
b) for(int j=0;j<anotherArray.length;j++)
The latter (b) is preferable, since it has less arithmetic operations on it. 后者(b)是优选的,因为它对其具有较少的算术运算。
Change 更改
for(int j=0;j<=anotherArray.length;j++)
to 至
for(int j=0;j<anotherArray.length;j++)
Since if it's <=
you'll be going out of bounds. 因为如果它<=
你将超出界限。
In Java (and most languages), arrays are zero-based. 在Java(和大多数语言)中,数组是从零开始的。 If you have an array of size N
then its indexes will be from 0
to N - 1
(total size of N
). 如果你有一个大小为N
的数组,那么它的索引将从0
到N - 1
(总大小为N
)。
change 更改
<=anotherArray.length
to 至
< anotherArray.length
For example, if array is 例如,如果是数组
int arr[] = new int[2];
arr.length; // it will be 2, which is [0] and [1] so you can't go upto <=length,
// that way you will access [2] which is invalid and so the exception
for(int j=0;j<anotherArray.length;j++)
instead of 代替
for(int j=0;j<=anotherArray.length;j++)
Because arrays are zero-based in Java. 因为数组在Java中是零基础的。
You will get ArrayIndexOutOfBoundsException when you try access the element that's out of Array limit. 当您尝试访问超出数组限制的元素时,您将获得ArrayIndexOutOfBoundsException 。
for(int j=0;j<anotherArray.length;j++) {
System.out.println(anotherArray[j]);
}
Why do you use this way to reverse your array in first place. 为什么你首先使用这种方式来反转你的数组。 Any way 无论如何
for(int j=0;j<=anotherArray.length;j++) should change to
for(int j=0;j<anotherArray.length;j++)
Consider this too, It is easy. 考虑一下这很容易。
int anArray[]={1,2,3,4,5,6};
int temp=0;
for (int i=0;i<anArray.length/2;i++){
temp=anArray[i];
anArray[i]=anArray[anArray.length-(1+i)];
anArray[anArray.length-(1+i)]=temp;
}
Now your array is reversed. 现在您的阵列已颠倒过来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.