Cod*_*123 5 java iterator hashtable hashmap hashset
我有两个哈希映射,我需要从其中一个中删除一个元素.这就是我现在正在做的事情.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到此错误,我无法看到代码有什么问题.有谁知道这里的问题是什么?
exception in thread "main" java.lang.IllegalStateException
at java.util.HashMap$HashIterator.remove(HashMap.java:910)
at DESPrac.main(DESPrac.java:59)
Run Code Online (Sandbox Code Playgroud)
你可能两次点击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
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1644 次 |
| 最近记录: |