现在,我有一个包含以下代码的程序:
while (arrayList.iterator().hasNext()) {
//value is equal to a String value
if( arrayList.iterator().next().equals(value)) {
// do something
}
}
就遍历ArrayList而言,我这样做是否正确?
我得到的错误是:
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)
我将显示其余的代码,但是它相当广泛,并且如果我没有正确地执行迭代,则我认为唯一的可能性就是我没有ArrayList
正确地初始化。
forEach
:方法stackoverflow.com/questions/16635398/...