在使用ArrayIndexOutOfBoundsException异常ArrayList的迭代器 [英] ArrayIndexOutOfBoundsException when using the ArrayList's iterator
问题描述
现在,我有一个包含一张code的,看起来像这样的程序:
而(arrayList.iterator()。规则hasNext()){
//值等于一个字符串值
如果(arrayList.iterator()。下一个()。等于(值)){
// 做一点事
}
}
我这样做对不对,只要通过迭代ArrayList中去?
我得到的错误是:
java.lang.ArrayIndexOutOfBoundsException:-1
在java.util.ArrayList.get(来源不明)
在main1.endElement(main1.java:244)
在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(来源不明)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(来源不明)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown资源)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(来源不明)
在com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(来源不明)
在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(来源不明)
在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(来源不明)
在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(来源不明)
在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(来源不明)
在com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(来源不明)
在javax.xml.parsers.SAXParser.parse(来源不明)
在javax.xml.parsers.SAXParser.parse(来源不明)
在main1.traverse(main1.java:73)
在main1.traverse(main1.java:102)
在main1.traverse(main1.java:102)
在main1.main(main1.java:404)
我会表现出code的休息,但它是pretty广泛的,如果我没有做正确的迭代中,我将承担唯一的可能就是我没有初始化的ArrayList
正常。
我这样做对不对,只要通过迭代ArrayList中去?
块引用>没有:通过调用
迭代器
在每次迭代两次,你得到新的迭代器的所有时间来写这个循环的最简单方法是使用,每个 一>结构:
的(一个String:ArrayList中)
如果(s.equals(值))
// ...至于
java.lang.ArrayIndexOutOfBoundsException:-1
块引用>您只是想从一个数组元素得到数
1
。在零开始计数。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.解决方案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异常ArrayList的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!