Enhanced 'For' Loop Causes an Arrayindexoutofboundsexception

Enhanced 'for' loop causes an ArrayIndexOutOfBoundsException

In this case, i will be assigned to each element in the array - it is not an index into the array.

What you want to do is:

for(int i : arrs)
{
System.out.println(i);
}

In your code, you're trying to select the integer at the array index referenced by the iteration object. In other words, your code is equivalent to:

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

ArrayIndexOutOfBound error when i used enhanced for loop

That is because you are accessing the elements of array a.

The loop

for (int i : a) {
System.out.println(i);
}

will print out the values: 1, 2, 3, 4.

You probably expected to get 0, 1, 2, 3 but that is not how the enhanced loop work.

Improvement

Instead of comparing the two array by hand, you can use the convenience method Arrays.equals():

public static void main(String[] args) {
int a[] = {1,2,3,4};
int b[] = {1,2,3,4};
boolean status = java.util.Arrays.equals(a, b);

if (status){
System.out.println("arrays are equal...");
} else {
System.out.println("arrays not equal...");
}
}

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.)

Enhanced For Loop Exception

for(int i : numbers)
{
System.out.print(numbers[i] + ", ");
}

i here is the elements in the array, not the indexes. It could be bigger than numbers.length.

For example, if numbers = {1,2,3,9} then i will be 1, 2, 3, 9. But its length is 4, so when you loop on the elements inside it, you're trying to do numbers[9] which exceeds its size.

You probably want to System.out.print(i + ", ");

I am getting java.lang.ArrayIndexOutOfBoundsException

Replace all your

for (int i : marks)

with

for (int i = 0; i < marks.length; i++)

and

for (int i : assessments)

with

for (int i = 0; i < assessments.length; i++)

When you use

for (int i : marks)

i is not the index of marks but the elements that marks contains:

50, 60, 65, 60, 65, 70, 55, 66, 60, 73, 65, 45, 68, 54

Unable to find the cause for java.lang.ArrayIndexOutOfBoundsException

You misunderstood the enhanced for loop. It iterates over the elements of the array, not their indices.

It should be :

for (int x : month_days) {
System.out.println(x);
}

How can I avoid ArrayIndexOutOfBoundsException or IndexOutOfBoundsException?

What is java.lang.ArrayIndexOutOfBoundsException / java.lang.IndexOutOfBoundsException?

The JavaDoc curtly states:

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.

What causes it to happen?

This exception means that you have tried to access an index in an
array or array backed list and that index does not exist.

Java uses 0 based indexes. That means all indexes start with 0 as
the index of the first element if it contains any elements.

The IndexOutOfBoundsException message is very explicit, and it usually takes the form of:

java.lang.IndexOutOfBoundsException: Index: 1, Size: 1

Where Index is the index that you requested that does not exist and Size is the length of the structure you were indexing into.

As you can see a Size: 1 means the only valid index is 0 and you were asking for what was at index 1.

For example, if you have an raw Array of objects or primitive types
the valid indexes are 0 to .length - 1, in the following example the valid indexes would be 0, 1, 2, 3,.

final String days[] { "Sunday", "Monday", "Tuesday" }
System.out.println(days.length); // 3
System.out.println(days[0]); // Sunday
System.out.println(days[1]); // Monday
System.out.println(days[2]); // Tuesday
System.out.println(days[3]); // java.lang.ArrayIndexOutOfBoundsException

This also applies to ArrayList as well as any other Collection classes that may be backed by an Array and allow direct access to the the index.

How to avoid the java.lang.ArrayIndexOutOfBoundsException / java.lang.IndexOutOfBoundsException?

When accessing directly by index:

This uses Guava to convert the raw primitive int[] array to an
ImmutableList<Integer>. Then it uses the Iterables class to safely
get the value at a particular index and provides a default value when
that index does not exist. Here I chose -1 to indicate an invalid
index value.

final List<Integer> toTen = ImmutableList.copyOf(Ints.asList(ints));
System.out.println(Iterables.get(toTen, 0, -1));
System.out.println(Iterables.get(toTen, 100, -1));

If you can't use Guava for some reason it is easy to roll your own function to do this same thing.

private static <T> T get(@Nonnull final Iterable<T> iterable, final int index, @Nonnull final T missing)
{
if (index < 0) { return missing; }
if (iterable instanceof List)
{
final List<T> l = List.class.cast(iterable);
return l.size() <= index ? l.get(index) : missing;
}
else
{
final Iterator<T> iterator = iterable.iterator();
for (int i = 0; iterator.hasNext(); i++)
{
final T o = iterator.next();
if (i == index) { return o; }
}
return missing;
}
}

When iterating:

Here is the idiomatic ways to iterate over a raw Array if you need
to know the index and the value:

This is susceptible to one off errors which are the primary causes
of an java.lang.ArrayIndexOutOfBoundsException:

Using a traditional for-next loop:

final int ints[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < ints.length; i++)
{
System.out.format("index %d = %d", i, ints[i]);
}

Using an enhanced for-each loop:

Here is the idiomatic way to iterate over a raw Array with the
enhanced for loop if you do not need to know the actual index:

for (final int i : ints)
{
System.out.format("%d", i);
System.out.println();
}

Using a type safe Iterator<T>:

Here is the safe way to iterate over a raw Array with the enhanced
for loop
and track the current index and avoids the possibility of
encountering an java.lang.ArrayIndexOutOfBoundsException.

This uses Guava to easily convert the int[] to something Iterable
every project should include it.

final Iterator<Integer> it = Ints.asList(ints).iterator();
for (int i = 0; it.hasNext(); i++)
{
System.out.format("index %d = %d", i, it.next());
}

If you can not use Guava or your int[] is huge you can roll your own ImmutableIntArrayIterator as such:

public class ImmutableIntArrayIterator implements Iterator<Integer>
{
private final int[] ba;
private int currentIndex;

public ImmutableIntArrayIterator(@Nonnull final int[] ba)
{
this.ba = ba;
if (this.ba.length > 0) { this.currentIndex = 0; }
else { currentIndex = -1; }
}

@Override
public boolean hasNext() { return this.currentIndex >= 0 && this.currentIndex + 1 < this.ba.length; }

@Override
public Integer next()
{
this.currentIndex++;
return this.ba[this.currentIndex];
}

@Override
public void remove() { throw new UnsupportedOperationException(); }
}

And use the same code as you would with Guava.

If you absolutely must have the ordinal of the item the following is the safest way to do it.

// Assume 'los' is a list of Strings
final Iterator<String> it = los.iterator();
for (int i = 0; it.hasNext(); i++)
{
System.out.format("index %d = %s", i, it.next());
}

This technique works for all Iterables. It is not an index parse, but it does give you the current position in the iteration even for things that do not have a native index.

The safest way:

The best way is to always use ImmutableLists/Set/Maps from Guava as
well:

final List<Integer> ili = ImmutableList.copyOf(Ints.asList(ints));
final Iterator<Integer> iit = ili.iterator();
for (int i = 0; iit.hasNext(); i++)
{
System.out.format("index %d = %d", i, iit.next());
}

Summary:

  1. Using raw arrays are difficult to work with and should be avoided in most cases. They are susceptible to sometimes subtle one-off errors which have plague new programmers even back to the days of BASIC

  2. Modern Java idioms use proper type safe collections and avoid using raw array structures if at all possible.

  3. Immutable types are preferred in almost all cases now.

  4. Guava is an indispensable toolkit for modern Java development.

for(int : array) index out of bounds exception

You reference arrays by providing an index of the location you want to get the value from. And these indexes start with zero. So to get the first value from the array, you would do bob[0]. What that for-loop is doing is automatically going through each element of the array, and one-by-one, putting the value into j. So to print out the value, you just do System.out.println(j);



Related Topics



Leave a reply



Submit