【问题标题】:NoSuchElementException while Iterating迭代时出现 NoSuchElementException
【发布时间】:2014-07-07 20:28:57
【问题描述】:

在过去的几个小时里,我一直被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("\n--------------------" + (String) iterator.next());

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

【问题讨论】:

  • 不能这样做。 hasNext 和 next 不在同一个线程上调用,这意味着 hasNext 将返回 true,因为尚未调用 next
  • 在调用runOnUiThread之前使用final String value = iterator.next();
  • @njzk2 解决了,但You have put me in this Situation :) 我会接受你的解决方案,如果你可以发布作为答案。陈述问题。谢谢

标签: java android iterator iteration


【解决方案1】:

这里存在并发问题:

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

因为hasNextnext 不是在同一个线程上调用的,所以不能保证调用next() 时确实会有下一个。

循环将在迭代器不前进的情况下运行多次,导致Runnables 的创建比迭代器中的项目更多,这反过来又导致next 被调用的次数超出了应有的次数。

您可以使用以下方法解决它:

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

在同一个线程中调用hasNextnext,确保每个hasNext 都与next 的调用立即匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-17
    • 2019-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多