java.lang.ArrayIndexOutOfBoundsException: 5
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24089254/
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
java.lang.ArrayIndexOutOfBoundsException: 5
提问by user3705481
I'm adding two arrays of the same length and I've written the code for it and no compile error shows up but when I run it I get:
我添加了两个相同长度的数组,我已经为它编写了代码并且没有出现编译错误,但是当我运行它时,我得到:
java.lang.ArrayIndexOutOfBoundsException: 5
java.lang.ArrayIndexOutOfBoundsException: 5
public class sumArray {
static double[] data1= {1.2, 2.3, 3.4, 5.1, 7.8};
static double[] data2= {5.3, 7.9, 2.1, 6.4, 9.2};
public static void main(String[] args){
sumArray();
}
public static double [] sumArray(){
double[] data3 = new double[data1.length];
for(int i = 0; i <= data1.length; i++){
data3[i] = data1[i] + data2[i];
}
return data3;
}
}
采纳答案by Frakcool
You're comparing from 0 - 5 (i.e. 6 elements, but your array has only 5), so you're going out of the bounds.
您正在比较 0 - 5(即 6 个元素,但您的数组只有 5 个),因此您超出了界限。
All you have to do is to go from 0 - data1.length - 1
like:
你所要做的就是从 0 开始——data1.length - 1
比如:
Change
改变
for (int i = 0; i <= data1.length; i++){
data3[i] = data1[i] + data2[i];
}
to
到
for (int i = 0; i < data1.length; i++){
data3[i] = data1[i] + data2[i];
}
or change it to:
或将其更改为:
for (int i = 0; i <= (data1.length - 1); i++){
data3[i] = data1[i] + data2[i];
}
回答by Edwin Torres D.Eng.
You are going off the end of the array. Change this line to:
你正在离开数组的末尾。将此行更改为:
for(int i = 0; i < data1.length; i++){
data1.length
is the size of the array. An array of size 5 has indexes from 0 - 4. So you should not let i
become greater than 4.
data1.length
是数组的大小。大小为 5 的数组具有 0 - 4 的索引。因此您不应让其i
大于 4。
回答by Roan
In your for
loop your counting variable can reach 5.
在您的for
循环中,您的计数变量可以达到 5。
for (int i = 0; i <= data1.length; i++){
data3[i] = data1[i] + data2[i];
}
This is because you say you want to go on as long as i
is equal to or smaller than data1.length.
这是因为您说只要i
等于或小于data1.length 就可以继续。
Thus because you have the =
sign in your expression it will reach 5 and that throws an error because there's only data at the positions 0 1 2 3 and 4.
因此,因为您=
的表达式中有符号,它会达到 5 并且会引发错误,因为位置 0 1 2 3 和 4 处只有数据。
Therefore your code should be:
因此你的代码应该是:
for(int i = 0; i < data1.length; i++){
data3[i] = data1[i] + data2[i];
}
Hope this explains it.
希望这能解释它。
回答by Abhishek Mishra
Array always start from zero position,
now just look at your code,data1 and data2 size is 5(i.e. data1[5],data2[5]) means it start from
data1[0]=1.2,data[1]=2.3,data[2]=3.4,data[3]=5.1,data[4]=7.8
same logic for data2
now check your for loop which says that
for(int i=0,i<=5,i++) means this loop get execute 6 times
i.e. data[0],data[1],data[2],data[3],data[4],data[5]
hope so you got it why it's throwing ArrayIndexOutOfBoundsException for 5th position
回答by Thimershen
The position of the last element in an array is = the number of elements in the array or the length of the array -1. Therefore the condition i <= data1.length must be changed to : i < data1.length.
数组中最后一个元素的位置=数组中元素的个数或数组的长度-1。因此条件 i <= data1.length 必须更改为:i < data1.length。