簡體   English   中英

Hashset迭代拋出Illegate狀態錯誤

[英]Hashset Iteration throws Illegate State error

我有兩個哈希映射,我需要從其中一個中刪除一個元素。 這就是我現在正在做的事情。

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)

你可能兩次點擊iterator.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
                }
            }
        }   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM