title: ArrayIndexOutOfBoundsException when using the ArrayList's iterator
body: <p>Right now, I have a program containing a piece of code that looks like this:</p>
  
  <pre><code>while (arrayList.iterator().hasNext()) {
       //value is equal to a String value
       if( arrayList.iterator().next().equals(value)) {
            // do something 
       }
  }
  </code></pre>
  
  <p>Am I doing that right, as far as iterating through the ArrayList goes?</p>
  
  <p>The error I am getting is:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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 <code>ArrayList</code> properly.</p>