哈希集迭代引发Illegate State错误



我有两个哈希映射,需要从其中一个中删除一个元素。这就是我现在正在做的事情。

for(Iterator<Byte> iterator = Ka.iterator(); iterator.hasNext();) {
                byte kaValue = iterator.next();
                byte potentialPIValue = (byte)(E1a + kaValue);
                for(byte actualPIValue : getPIs) {                       
                    if (potentialPIValue != actualPIValue )                         
                        iterator.remove();
                }
            }   

然而,我遇到了这个错误,我看不出代码出了什么问题。有人知道这里的问题是什么吗?

 exception in thread "main" java.lang.IllegalStateException
at java.util.HashMap$HashIterator.remove(HashMap.java:910)
at DESPrac.main(DESPrac.java:59)

您可能两次点击迭代器.remove()语句而没有移动到下一个元素,因为您在内部循环中调用它。

尝试

       for(Iterator<Byte> iterator = Ka.iterator(); iterator.hasNext();) {
            byte kaValue = iterator.next();
            byte potentialPIValue = (byte)(E1a + kaValue);
            for(byte actualPIValue : getPIs) {                       
                if (potentialPIValue != actualPIValue ){                         
                    iterator.remove();
                    break; // Exit the inner loop
                }
            }
        }   

相关内容

最新更新