Java:数组索引越界异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3951252/
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: Array Index Out of Bounds Exception
提问by Arly
System.out.print("Enter an integer: ");
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int lArray = x - 2;
int[] newArray = new int[lArray];
System.out.println("Let's display all possible integers...");
for (int i = 0; i <= newArray.length; i++) {
newArray[i] = i + 2;
System.out.print(newArray[i] + " ");
}
I've just started Java recently, but I sure that if I coded similarly in another language, I would face the same problem. This is an excerpt from an application where it lists all the prime numbers up until the user's input.
我最近才开始使用 Java,但我确信如果我用另一种语言进行类似的编码,我会面临同样的问题。这是一个应用程序的摘录,其中列出了用户输入之前的所有素数。
The reason why x-2 is used as the definition of lArray is because the length of the array will be all the integers from 2 until the number {2, 3, 4, 5... x}.
之所以使用 x-2 作为 lArray 的定义是因为数组的长度将是从 2 到数字 {2, 3, 4, 5 ... x} 的所有整数。
I noticed that for the line
我注意到这一行
for (int i = 0; i <= newArray.length; i++) {
if I change i <= newArray
to i < newArray
, the code works without error. However, the user's input, x, is left out which is a problem if x is prime.
如果我更改i <= newArray
为i < newArray
,则代码可以正常工作。然而,用户的输入 x 被遗漏了,如果 x 是素数,这是一个问题。
回答by codaddict
You should use <
and not <=
in:
您应该使用<
而不是<=
在:
for (int i = 0; i <= newArray.length; i++)
^^
If foo
any array, valid index of foo
are [0,foo.length-1]
如果有foo
任何数组,有效索引foo
是[0,foo.length-1]
Using foo.length
as an index will cause ArrayIndexOutofBoundsException
.
使用foo.length
作为索引会导致ArrayIndexOutofBoundsException
。
And also lArray
which contains number of natural numbers <=x
but excluding only onenumber 1
, its value should be x-1
and not x-2
.
并且lArray
其中包含多个自然数<=x
但只排除一个数字1
,它的值应该是x-1
而不是x-2
。
回答by Ani
Change the array length to (x - 1)
instead, and go with the <
condition, which you've already found is necessary to avoid the out-of-bounds exception.
将数组长度改为(x - 1)
改为,并使用<
您已经发现的条件,以避免越界异常。
The reason you need an array that is 1 element larger than what you're currently using is because there are (n - 1)
candidates that must be considered between 2 and n , not (n - 2)
.
您需要一个比您当前使用的元素大 1 个元素的数组的原因是(n - 1)
,必须考虑 2 和 n 之间的候选对象,而不是(n - 2)
。
For example, there are two candidates less than or equal to three (2 and 3), both of which, coincidentally, happen to be prime.
例如,有两个小于或等于 3 的候选者(2 和 3),巧合的是,它们都是素数。
回答by Pablo Santa Cruz
You need to use:
您需要使用:
int lArray = x - 1;
And change your condition to use <
instead of <=
.
并更改您的条件以使用<
而不是<=
.
In Java as in C/C++, arrays are ZERO based. So your array of Nvalues will go from index 0to N-1.
在 Java 和 C/C++ 中,数组都是基于零的。所以你的N值数组将从索引0到N-1。
Taking your example: {2, 3, 4, 5... x}
.
以你为例:{2, 3, 4, 5... x}
。
You will need N-1
values to store all positive numbers but 1in an integer array. So, if N
equals to 4, your array will be:
您将需要N-1
值将除1 之外的所有正数存储在整数数组中。所以,如果N
等于4,你的数组将是:
newArray[0] = 2;
newArray[1] = 3;
newArray[2] = 4;
Hence, array lenght must be 3(N-1).
因此,数组长度必须为3( N-1)。
回答by a1ex07
for (int i = 0; i <= newArray.length; i++) //should be <, not <=
for (int i = 0; i < newArray.length; i++)