java.lang.ArrayIndexOutOfBoundsException: 4 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19205240/
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: 4 Error
提问by Sebastien Bah
I'm new to coding and I've been writing this code and trying to make it work but every time I run it it crashes. I've looked things up and will writing this code I've followed java's website on how to properly write down code as well as this site.
我是编码新手,我一直在编写这段代码并试图让它工作,但每次运行它时它都会崩溃。我已经查过资料并将编写此代码我已关注 java 的网站,了解如何正确编写代码以及本网站。
Anyways, it would be greatly appreciated if someone can explain to me why this is not working because it seems to me like the logic is there but I don't get why it crashes.
无论如何,如果有人能向我解释为什么这不起作用,我将不胜感激,因为在我看来逻辑就在那里,但我不明白它为什么会崩溃。
My code:
我的代码:
import java.util.Scanner;
import java.lang.String;
import java.util.*;
public class Question1
{
public static void main(String[] args)
{
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter either letters or numbers and I'll magically tell you if they are consecutive :D");
String inputedString= keyboard.nextLine();
boolean consecutiveOrNot=isConsecutive(inputedString);
System.out.println("Drum rolls...... Is it consecutive: "+ consecutiveOrNot); //Problem with this line?
}
public static boolean isConsecutive(String inputedString)
{
//Storing string's units into an array and converting to UpperCase if necessary
//and storing string's numerical value into the variable 'arrayCharToInt'
char[] charIntoArray= new char[inputedString.length()];
int[] arrayCharToInt= new int[inputedString.length()];
for (int i=0;i<inputedString.length();i++ )
{
charIntoArray[i]=inputedString.charAt(i);
if (Character.isLetter(charIntoArray[i]) && Character.isLowerCase(charIntoArray[i]))
{
charIntoArray[i]= Character.toUpperCase(charIntoArray[i]);
}
arrayCharToInt[i]=(int) charIntoArray[i];
}
// The next if statements and the methods that they call are used to verify
//that the content of the initial string is either letters or numbers, but not both together
boolean[] continuous= new boolean[arrayCharToInt.length];
boolean[] testContNumbersDecreasing= new boolean[arrayCharToInt.length];
boolean[] testContNumbersIncreasing= new boolean[arrayCharToInt.length];
boolean[] testContLettersDecreasing= new boolean[arrayCharToInt.length];
boolean[] testContLettersIncreasing= new boolean[arrayCharToInt.length];
Arrays.fill(continuous, true);
if (lowestValue(arrayCharToInt)>=65 && highestValue(arrayCharToInt)<= 90)
{
for (int x=0;x<arrayCharToInt.length ;x++ )
{
testContLettersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -25));
testContLettersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -25));
}
return (Arrays.equals(continuous,testContLettersIncreasing) || Arrays.equals(continuous,testContLettersDecreasing));
}
else if ((lowestValue(arrayCharToInt) >= 48) && (highestValue(arrayCharToInt)<= 57))
{
for (int x=0;x<arrayCharToInt.length ;x++ )
{
testContNumbersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -9));
testContNumbersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -9));
}
return (Arrays.equals(continuous,testContNumbersIncreasing) || Arrays.equals(continuous,testContNumbersDecreasing));
}
else
{
return false;
}
}
public static int lowestValue(int[] array)
{
int lowest=array[0];
for (int counter=0; counter< array.length; counter++)
{
if( lowest>array[counter])
lowest= array[counter];
}
return lowest;
}
public static int highestValue(int[] array)
{
int highest=array[0];
for (int counter=0; counter< array.length; counter++)
{
if( highest<array[counter])
highest= array[counter];
}
return highest;
}
}
The main method seems to be fine because it put everything in the isConsecutive method as a comment except for 'return true;' and indeed the program ran and printed true. So I know the problem lies somewhere in the second method.
main 方法似乎没问题,因为它将 isConsecutive 方法中的所有内容作为注释,除了 'return true;' 确实程序运行并打印为true。所以我知道问题出在第二种方法中。
If there's anything that I did not do right please tell me and that would be greatly appreciated. After all I'm still learning.
如果有任何我做错的地方,请告诉我,我们将不胜感激。毕竟我还在学习。
Thanks
谢谢
采纳答案by musical_coder
All of your calls to arrayCharToInt[x+1]
are going to go out of bounds on the last iteration of the loop they're in (for example, if arrayCharToInt.length
equals 5, the highest that x
is going to go is 4. But then x+1
equals 5, which is out of bounds for an array with five cells). You'll need to put in some sort of if( x == arrayCharToInt.length - 1)
check.
arrayCharToInt[x+1]
在它们所在的循环的最后一次迭代中,您的所有调用都将超出范围(例如,如果arrayCharToInt.length
等于 5,则将达到的最高x
值是 4。但随后x+1
等于 5,即超出范围具有五个单元格的数组的边界)。您需要进行某种if( x == arrayCharToInt.length - 1)
检查。
回答by Balaji Krishnan
in the method isConsecutive inside the for loop: for (int x=0;x<arrayCharToInt.length ;x++ )
, you have used arrayCharToInt[x+1]
在 for 循环中的 isConsecutive 方法中: for (int x=0;x<arrayCharToInt.length ;x++ )
,您已经使用了arrayCharToInt[x+1]
if the arrayCharToInt lenth is 4 , then you have arrayCharToInt [0]
to arrayCharToInt [3]
.
如果arrayCharToInt lenth为4,那么你必须arrayCharToInt [0]
给arrayCharToInt [3]
。
now consider this statement:arrayCharToInt[x+1]
when x is 3 this statement will evalueate to arrayCharToInt[4]
resulting in array index out of bounds exception
现在考虑以下语句:arrayCharToInt[x+1]
当 x 为 3 时,此语句将评估为arrayCharToInt[4]
导致数组索引越界异常
回答by Aathil Ahamed
This error throw when something went wrong in the Array calling function. You got the length and make it print. for eg:
当 Array 调用函数出现问题时会抛出此错误。你得到了长度并打印出来。例如:
int a[] = {1,2,3,4}
Length of this array is,
这个数组的长度是,
int length = a.length
So length = 4
but highest index is 3, not 4. That means index of the array started with 0. So you have to print:
所以length = 4
但最高索引是3,而不是 4 。这意味着数组的索引从 0 开始。所以你必须打印:
arr[length-1];
In your program,
在你的程序中,
x == arrayCharToInt.length - 1