Arrayindexoutofboundsexception When Iterating Through All the Elements of an Array

ArrayIndexOutOfBoundsException when iterating through an array

In general, if the value of 'i' is equal to or less than the length of (array_size - 1), a[i] would be valid.

However, in this method, public static int getPointsStrikeSpare(int i, char[] inputChars) one can see that a[i+1] and a[i+2] end up getting queried in different code paths in the if-else structure. So, if your 'i' index already reached the end of the array, this is going to be Out of Bounds.

Ex: X-/X5-8/9-X811-4/X
The size of this is 18. So, the legal values to refer are from 0-17 i.e., a[0] to a[17], since indexing in array starts from 0. Towards the end of the program, index counter 'i' becomes 17.

if(inputChars[i] == 'X') {               // VALID
if(inputChars[i+1] == '-') { // INVALID
points += 0;
} else if (inputChars[i+1]== '/') { // INVALID
points += 10;
} else {
points += Character.getNumericValue(inputChars[i+1]); // INVALID
}
if(inputChars[i+2] == '-') { // INVALID

You get the general idea here.

What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

Your first port of call should be the documentation which explains it reasonably clearly:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

So for example:

int[] array = new int[5];
int boom = array[10]; // Throws the exception

As for how to avoid it... um, don't do that. Be careful with your array indexes.

One problem people sometimes run into is thinking that arrays are 1-indexed, e.g.

int[] array = new int[5];
// ... populate the array here ...
for (int index = 1; index <= array.length; index++)
{
System.out.println(array[index]);
}

That will miss out the first element (index 0) and throw an exception when index is 5. The valid indexes here are 0-4 inclusive. The correct, idiomatic for statement here would be:

for (int index = 0; index < array.length; index++)

(That's assuming you need the index, of course. If you can use the enhanced for loop instead, do so.)

ArrayIndexOutOfBoundsException when using the ArrayList's iterator

Am I doing that right, as far as iterating through the Arraylist goes?

No: by calling iterator twice in each iteration, you're getting new iterators all the time.

The easiest way to write this loop is using the for-each construct:

for (String s : arrayList)
if (s.equals(value))
// ...

As for

java.lang.ArrayIndexOutOfBoundsException: -1

You just tried to get element number -1 from an array. Counting starts at zero.

ArrayIndexOutOfBoundsException inside for loop

The problem is that you are assigning to arrayOfNumbers which you initialize to only have one element. Did you mean to assign to evenString instead? evenString is initialized with the correct array size for this loop.

You could also just declare arrayOfNumbers to be the same size as the evenString array. For example:

int[] arrayOfNumbers = new int[arraySize];                  
for (int i=0; i < arraySize; i++){

String w = s.substring(x,y);

arrayOfNumbers[i] = Integer.parseInt(w); //THIS LINE IS FAILING *********

if(i == arraySize - 2){
x = x + 9;
y = y + (numSize % 9);
}
else{
y = y + 9;
x = x + 9;
}
}

java.lang.ArrayIndexOutOfBoundsException - Fill new Array with looped Data from other Array

This is because the dasGeradeArray has half of the indexes that dasArray has. You shouldn't use the same i variable. You can add another variable that you increment each time you add something to dasGeradeArray:

/

/CREATE NEW ARRAY WITH ALL EVEN NUMBERS AND THE LENGHT OF COUNT
int []dasGeradeArray = new int[count];
int dasGeradeCounter = 0
for(int i = 0; i < arrayLenght; i++){
if (dasArray[i] % 2 == 0) {
dasGeradeArray[dasGeradeCounter]= dasArray[i];
dasGeradeCounter++;
}

Java throws ArrayIndexOutOfBoundsException if using a regular if loop, but not if using an enhanced for loop. Why?

You are using for loop in wrong way. You cannot be sure that refinedSplitLine contains an element. Just replace following loop:

for (int i = 0; i<4; i++) {
System.out.println(refinedSplitLine[i]);
}

With this:

for (int i = 0; i<refinedSplitLine.length; i++) {
System.out.println(refinedSplitLine[i]);
}

For each works fine because it iterate through all elements of array, if it's empty it just doesn't print anything. But normal loop in your case try to get element from array which index doesn't exists that's why you have an exception.

ArrayIndexOutOfBoundsException iterating over 2D arrays in Java

Try this one :

for(int i = 0; i < values.length; i++){ 
for(int j = 0; j < values[i].length; j++){// replaced j in (i < values[i].length) by i in (j < values[i].length)
if (values[i][j] == 0){//if empty
for(int next = 1; next <= 9; next++){


Related Topics



Leave a reply



Submit