2010-12-16 100 views
1

我在列出ArrayList時遇到問題。我的代碼:問題arrayList類

Iterator<String> iterator = (Iterator<String>) myList.iterator(); 
while (iterator.hasNext()) {//List tagListAux 
     System.out.println("Test -> "+iterator.next()); 
     if(iterator.next().equalsIgnoreCase(myString)){ 
     flag = true; 
} 

myList.add(myString); 

我得到異常:

java.util.NoSuchElementException 
    at java.util.AbstractList$Itr.next(AbstractList.java:350) 
    at myFileinJava(myFileinJava.java:148) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

我在做什麼錯?在還有併發問題之前,解決了更改迭代器聲明的問題。
在此先感謝

回答

6

要檢查,有一個下一個元素是有後調用next()中而塊兩次 [注意不是兩片完全相同]

取代

Iterator<String> iterator = (Iterator<String>) myList.iterator(); 
while (iterator.hasNext()) {//List tagListAux 
     System.out.println("Test -> "+iterator.next()); 
     if(iterator.next().equalsIgnoreCase(myString)){ 
     flag = true; 
} 

for(String str: myList){ 
     System.out.println("Test -> "+str); 
     if(str.equalsIgnoreCase(myString)){ 
     flag = true; 
} 
+0

PFF是的,你是對的!!多麼愚蠢的東西!謝謝 – 2010-12-16 10:29:36

4

您有兩次!一旦調用它,然後用它的值:String value = iterator.next();

2
while (iterator.hasNext()) {//List tagListAux 
    String item = iterator.next(); 
    System.out.println("Test -> "+item); 
    if(item.equalsIgnoreCase(myString)) { 
     flag = true; 
    } 
    myList.add(myString); 
} 
1

只需使用foreach(在Java 5+)環

0

當您使用梅索德Iterator.next(),你增加你的ArrayList和你的指針在同一時間做兩次,而不檢查下一個元素是否存在(用於第二次下一次調用)。這就是爲什麼你得到'NoSuchElementException'。

你必須做這樣的事情:

while (iterator.hasNext()) {//List tagListAux 
     Object currentElement = iterator.next(); 
     System.out.println("Test -> "+ currentElement); 
     if(currentElement .equalsIgnoreCase(myString)){ 
     flag = true; 
} 

Anhuin

1

我想你應該改變這樣的代碼:

while (iterator.hasNext()) {//List tagListAux 
     String s = iterator.next().toString(); 
     System.out.println("Test -> "+ s); 
     if(s.equalsIgnoreCase(myString)){ 
      flag = true; 
     } 

} 
1
for(String str: myList){ 

    System.out.println("Test -> "+str); 

    if(str.equalsIgnoreCase(myString)){ 

     flag = true; 
    } 
} 

與使用的最佳途徑尊重速度也