简体   繁体   English

Java“null”值从未分配给我的Runnable

[英]Java “null” value never assigned to my Runnable

So I have this code: 所以我有这个代码:

    for(Runnable loadTask : mMeshLoadMap.values())
    {
        if(loadTask != null)
        {
            loadTask.run();
            loadTask = null;
        }
    }

My problem is the loadTask = null statement is never executed, Android Studio says "The value "null" assigned to loadTask is never used"... 我的问题是从不执行loadTask = null语句,Android Studio说“从不使用赋值给loadTask的值”null“...

Plus when I set a breakpoint at that line the debugger never reaches it. 另外,当我在该行设置断点时,调试器永远不会到达它。 It can reach loadTask.run() but not loadTask = null And I am sure that loadTask.run() doesn't block soo long 它可以到达loadTask.run()而不是loadTask = null我确信loadTask.run()不会阻塞soo long

It certainly can be assigned, but it is pointless. 它当然可以分配,但它毫无意义。 You'd only be nulling the reference named loadTask (which is out of scope outside the block). 您只需将名为loadTask的引用置loadTask (它超出块的范围)。 You cannot update the reference in the array. 您无法更新阵列中的引用。 The For-Each Loop says (in part) For-Each Loop说(部分)

The for-each loop hides the iterator, so you cannot call remove. for-each循环隐藏了迭代器,因此您无法调用remove。 Therefore, the for-each loop is not usable for filtering. 因此,for-each循环不可用于过滤。 Similarly it is not usable for loops where you need to replace elements in a list or array as you traverse it. 类似地,它不适用于需要在遍历时替换列表或数组中的元素的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM