开发者

Problem arrayList class

I am having problems while listing an ArrayList. My code:

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);

The exception I am getting:

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)

What am I doing wrong?? Before I was having also concurrency problems, which were solved changing the iterator declaration.

Thanks in advance


You are calling next() twice in while block after checking that there is one next element is there [note not two exactly]

replace

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;
}  

with

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


You have iterator.next() twice! Call it once and then use its value: String value = iterator.next();


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


Just use foreach (Java 5+) loop


I think you should change your code like that :

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

}


for(String str: myList){

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

    if(str.equalsIgnoreCase(myString)){

        flag = true;
    }
}

Is the best way of using with respect to speed too


when you use the methode Iterator.next(), you increment the pointer in your Arraylist and you are doing it twice in the same while without checking if the next Element exists (for the second next call). That's why you got a 'NoSuchElementException'.

You must do something like that :

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

Anhuin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜