迭代时的NoSuchElementException

时间:2014-07-07 20:28:57

标签: java android iterator iteration

过去几个小时我被Iterator击中。

当我执行 Iterator 时,我得到 NoSuchElementException

代码

    new Thread() {
        @Override
        public void run() {
            System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

            final HashSet<String> mList = new HashSet<String>();
            for (SomeList sList : refList) { // <-- 72 Items
                if (sList.isTrue()) {
                    mList.add(sList.getName());
                }
            }
            System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww");

            System.out.println("sList " + sList.size()); // <-- 3 Items

            final Iterator<String> iterator = mList.iterator();
            while (iterator.hasNext()) { // Check if has next element
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("zzzzzzzzzzzzzz");
                        System.out.println("\n-------------------- " + (String) iterator.next()); //move to next element
                    }
                });
            }
        }
    }.start();

logcat的

    D/dalvikvm(25527): start new thread
    D/dalvikvm(25527): threadid=13: notify debugger
    D/dalvikvm(25527): threadid=13 (Thread-4167): calling run()
    I/System.out(25527): xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    I/System.out(25527): wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
    I/System.out(25527): mList 3 <-- Size of List
    I/System.out(25527): yyyyyyyyyyyyyyyyyyyyyy
    I/System.out(25527): yyyyyyyyyyyyyyyyyyyyyy

    // Prints around 80 times removed for better readability

    I/System.out(25527): yyyyyyyyyyyyyyyyyyyyyy
    I/System.out(25527): yyyyyyyyyyyyyyyyyyyyyy
    I/System.out(25527): zzzzzzzzzzzzzz
    I/System.out(25527): -------------------- abc <-- data 1
    I/System.out(25527): yyyyyyyyyyyyyyyyyyyyyy
    I/System.out(25527): zzzzzzzzzzzzzz
    I/System.out(25527): -------------------- efg <-- data 2
    I/System.out(25527): zzzzzzzzzzzzzz
    I/System.out(25527): -------------------- hij <-- data 3
    I/System.out(25527): zzzzzzzzzzzzzz
    D/AndroidRuntime(25527): Shutting down VM
    W/dalvikvm(25527): threadid=1: thread exiting with uncaught exception (group=0x41c5c9a8)
    D/dalvikvm(25527): threadid=13: exiting
    D/dalvikvm(25527): threadid=13: bye!
    E/AndroidRuntime(25527): FATAL EXCEPTION: main
    E/AndroidRuntime(25527): java.util.NoSuchElementException
    E/AndroidRuntime(25527):    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:794)
    E/AndroidRuntime(25527):    at java.util.HashMap$KeyIterator.next(HashMap.java:819)
    E/AndroidRuntime(25527):    at xxx.xxx.xxxxxx.models.YYYYY$1$1.run(YYYYY.java:62)  <-- Here is the exception.
    E/AndroidRuntime(25527):    at android.os.Handler.handleCallback(Handler.java:725)
    E/AndroidRuntime(25527):    at android.os.Handler.dispatchMessage(Handler.java:92)
    E/AndroidRuntime(25527):    at android.os.Looper.loop(Looper.java:153)
    E/AndroidRuntime(25527):    at android.app.ActivityThread.main(ActivityThread.java:5299)
    E/AndroidRuntime(25527):    at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(25527):    at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime(25527):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    E/AndroidRuntime(25527):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    E/AndroidRuntime(25527):    at dalvik.system.NativeStart.main(Native Method)

我的代码中的第62行

  

System.out.println(&#34; \ n --------------------&#34; +(String)iterator.next());

在发布此问题之前,我搜索了NoSuchElementException, Iterator,尝试了大部分结果,但没有成功。

1 个答案:

答案 0 :(得分:2)

这里存在并发问题:

while (iterator.hasNext()) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            (String) iterator.next();
        }
    });
}

由于hasNextnext未在同一个帖子中调用,因此无法保证在您拨打next()时确实存在下一个。

循环将运行几次而迭代器不会被提前,导致Runnable的创建比迭代器中的项更多,这反过来会导致next被调用的次数超过应有的次数。

您可以使用以下方法解决问题:

while (iterator.hasNext()) {
    final String nextValue = (String) iterator.next();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // Do stuff with nextValue
        }
    });
}

在同一个帖子中调用hasNextnext,确保通过调用hasNext

立即匹配每个next
相关问题