Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayIndexOutOfBoundsException when using the ArrayList's iterator [duplicate]

Right now, I have a program containing a piece of code that looks like this:

while (arrayList.iterator().hasNext()) {      //value is equal to a String value      if( arrayList.iterator().next().equals(value)) {           // do something       } } 

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

The error I am getting is:

java.lang.ArrayIndexOutOfBoundsException: -1     at java.util.ArrayList.get(Unknown Source)     at main1.endElement(main1.java:244)     at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)     at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)     at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)     at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)     at javax.xml.parsers.SAXParser.parse(Unknown Source)     at javax.xml.parsers.SAXParser.parse(Unknown Source)     at main1.traverse(main1.java:73)     at main1.traverse(main1.java:102)     at main1.traverse(main1.java:102)     at main1.main(main1.java:404) 

I would show the rest of the code, but it's pretty extensive, and if I am not doing the iteration correctly, I would assume the only possibility is that I am not initializing the ArrayList properly.

like image 930
This 0ne Pr0grammer Avatar asked Jul 14 '11 22:07

This 0ne Pr0grammer


People also ask

Why am I getting an ArrayIndexOutOfBoundsException?

The ArrayIndexOutOfBoundsException is one of the most common errors in Java. It occurs when a program attempts to access an invalid index in an array i.e. an index that is less than 0, or equal to or greater than the length of the array.

How do you resolve ArrayIndexOutOfBoundsException?

In order to avoid the java. lang. ArrayIndexOutOfBoundsException, you should always do the bound check before accessing array element e.g. Always remember that the array index starts at 0 and not 1 and an empty array has no element in it.

At which point of execution does the ArrayIndexOutOfBoundsException occur?

The ArrayIndexOutOfBoundsException occurs whenever we are trying to access any item of an array at an index which is not present in the array. In other words, the index may be negative or exceed the size of an array.

What is ArrayIndexOutOfBoundsException in Java when does it occur?

ArrayIndexOutOfBoundsException occurs when we access an array, or a Collection, that is backed by an array with an invalid index. This means that the index is either less than zero or greater than or equal to the size of the array.


1 Answers

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.

like image 138
Fred Foo Avatar answered Oct 11 '22 13:10

Fred Foo