2011-07-14 225 views
102

現在,我已經包含了一段代碼,看起來像這樣的程序:ArrayIndexOutOfBoundsException異常時,ArrayList的迭代器

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

+0

在java 8中,你可以使用'forEach'方法:http://stackoverflow.com/questions/16635398/java-8-iterable-foreach-vs-foreach-loop –

回答

228

我是否這樣做,只要迭代Arraylist去?

否:通過在每次迭代中調用iterator兩次,您總是獲得新的迭代器。

寫這個循環的最簡單方法是使用for-each結構:您剛剛試圖從陣列獲取元素編號-1

for (String s : arrayList) 
    if (s.equals(value)) 
     // ... 

至於

java.lang.ArrayIndexOutOfBoundsException: -1

。計數從零開始。

+1

用於每一個,它更容易。也有可能你再次調用arrayList.iterator()。next()並跳過條目。 – 2011-07-14 22:34:05

+0

@ larsmans非常感謝你。我完全忘了你可以用數組List來做到這一點。但是,我試着用我的代碼,我仍然得到相同的錯誤。所以我認爲這是一個問題,我之前在代碼中添加到arrayList,所以我現在看看地址。不過,非常感謝你提醒我。 –

+0

愛每個運營商。我總是用紅寶石般的東西... 'do array.each | s | 除非(s.nil?) 結束 結束' –

37
List<String> arrayList = new ArrayList<String>(); 
for (String s : arrayList) { 
    if(s.equals(value)){ 
     //do something 
    } 
} 

for (int i = 0; i < arrayList.size(); i++) { 
    if(arrayList.get(i).equals(value)){ 
     //do something 
    } 
} 

但要小心ArrayList中可容納空值。所以比較 - 應該是

value.equals(arrayList.get(i)) 

當你確信值不爲空,或者您應檢查是否給定的元素爲null。

+0

感謝您的意見。 :) –

7

除了larsmans回答(誰的確是正確的),get()方法的調用中的異常,因此您發佈的代碼不是導致錯誤的代碼。

8

你也可以對循環做,你會爲一個數組但不是數組[我]你會使用list.get(I)

for (int i = 0; i < list.size(); i++) { 
    System.out.println(list.get(i)); 
} 
141

雖然我同意接受的答案通常是最好的解決方案,肯定更容易使用,我注意到沒有人顯示迭代器的正確用法。因此,這裏是一個簡單的例子:

Iterator<Object> it = arrayList.iterator(); 
while(it.hasNext()) 
{ 
    Object obj = it.next(); 
    //Do something with obj 
} 
+12

我覺得更準確地回答這個問題,因爲它是一個迭代器的例子,而不是一個替代解決方案。 – withoutclass

+1

謝謝你的富有洞察力的迴應。對於(...)迭代*通常是最好的解決方案,但並非總是如此。今天,我碰巧在尋找顯式託管的迭代器語法,在這裏。 –

+1

美麗而有用的答案。 – Fattie

10

您也可以使用這樣的:

for(Iterator iterator = arrayList.iterator(); iterator.hasNext();) { 
x = iterator.next(); 
//do some stuff 
} 

它是一個很好的做法,演員和使用對象。 例如,如果'arrayList'包含'Object1'對象的列表。然後,我們可以重新寫代碼:

for(Iterator iterator = arrayList.iterator(); iterator.hasNext();) { 
x = (Object1) iterator.next(); 
//do some stuff 
} 
4

有效的方式來重複你的ArrayList其次是這個link。這種類型將提高在迭代

int size = list.size(); 

for(int j = 0; j < size; j++) { 
    System.out.println(list.get(i)); 
} 
2

使用iterator如果你迭代器創建之後添加元素添加到集合,然後它會拋出concurrentmodificaionexception沒有故障安全例如迭代循環的性能。此外它不是線程安全的,你必須使它在外部線程安全。

因此,最好使用for循環的每個結構。它至少是安全的。

相關問題