问题arrayList类

时间:2010-12-16 10:23:29

标签: java 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)

我做错了什么?在我遇到并发问题之前,已经解决了改变迭代器声明的问题 提前致谢

7 个答案:

答案 0 :(得分:6)

在检查到存在下一个元素后,你在while块中调用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;
}

答案 1 :(得分:4)

你有iterator.next()两次!调用一次,然后使用其值:String value = iterator.next();

答案 2 :(得分:2)

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

答案 3 :(得分:1)

只需使用foreach(Java 5+)循环

答案 4 :(得分:1)

我认为你应该改变你的代码:

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

}

答案 5 :(得分:1)

for(String str: myList){

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

    if(str.equalsIgnoreCase(myString)){

        flag = true;
    }
}

关于速度的最佳使用方式也是

答案 6 :(得分:0)

当你使用methode Iterator.next()时,你在Arraylist中递增指针,你在同一时间做两次,而不检查下一个Element是否存在(对于第二个下一个调用)。这就是你得到'NoSuchElementException'的原因。

你必须做那样的事情:

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

Anhuin