简体   繁体   English

Hashset迭代抛出Illegate状态错误

[英]Hashset Iteration throws Illegate State error

I have two hash maps and I need to remove an element from one of them. 我有两个哈希映射,我需要从其中一个中删除一个元素。 This is what I am doing right now. 这就是我现在正在做的事情。

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();
                }
            }   

However i get this error and I am unable to see what's wrong with the code. 但是我收到此错误,我无法看到代码有什么问题。 Would anyone know what the problem here is? 有谁知道这里的问题是什么?

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

You're probably hitting the iterator.remove() statement twice without moving to the next element, since you're calling it in your interior loop. 你可能两次点击iterator.remove()语句而不移动到下一个元素,因为你在内部循环中调用它。

Try 尝试

       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
                }
            }
        }   

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

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