gpt4 book ai didi

java - 迭代器删除/编辑是安全的,但是我还需要编辑以下所有迭代

转载 作者:行者123 更新时间:2023-12-01 13:59:17 26 4
gpt4 key购买 nike

我在 stackflow 中阅读了一些有关 ConcurrentModificationException 的内容,我的实际更新似乎不是问题,这可能是我的设计中的问题,或者我需要一种我尚未学习的技术。

示例情况:我的迭代器沿着位置标记运行。然后可以执行一个操作来移动标记(例如插入字符串)。所有大于当前位置的标记也必须移动以保持正确性。

任务:如何在迭代器不爆炸的情况下更新剩余标记?我可以刷新迭代器,或者中断并重新开始循环吗?

以下代码是从我的工作中抽象出来的。

 public void innerLoop(Boolean b) {
//An Example of what I'm working with
HashMap<String, HashSet<Integer>> map = new HashMap<String, HashSet<Integer>>() {
{
put("Nonce",
new HashSet<Integer>() {

{
add(1);
add(2);
add(3);
add(4);
add(5);
}
});
}
};

//for each key
for (String key: map.keySet()) {
HashSet<Integer> positions = map.get(key);

//for each integer
for (Iterator<Integer> it = positions.iterator(); it.hasNext();) {
Integer position = it.next();

System.out.println("position =" + position);
//(out of scope) decision requiring elements from the outter loops
if (new Random().nextBoolean()&&b) {
//shift position by +4 (or whatever)
//and every other (int >= position)
System.out.println("Shift " + position + " by 4");
Integer shift = 4;
update(position,
shift,
positions);
it.remove();
}
}
}
}

public void update(Integer current,
Integer diff,
Set<Integer> set) {

if (set != null) {
HashSet<Integer> temp = new HashSet<Integer>();
for (Integer old: set) {
if (old >= current) {
temp.add(old);
System.out.println(old + "Added to temp");
}
}

for (Integer old: temp) {
set.remove(old);
System.out.println(old + "removed");
set.add(old + diff);
System.out.println((old + diff) + "Added");
}
}
}
<小时/>

使用 Garrett Hall 解决方案进行编辑

 public void nestedloops() {

HashMap<String, HashSet<Integer>> map = new HashMap<String, HashSet<Integer>>() {
{
put("Hello",
new HashSet<Integer>() {

{
add(5);
add(2);
add(3);
add(4);
add(1);
add(6);
}
});
}
};

//for each key
for (String key: map.keySet()) {
ArrayList<Integer> positions = new ArrayList<Integer>(map.get(key));
//for each integer
for (int i = 0; i < positions.size(); i++) {
Integer position = positions.get(i);
System.out.println("[" + i + "] =" + position);
//out of scope decision
if (new Random().nextBoolean()) {
//shift position by +4
//and every other (int >= position)
System.out.println("Shift after " + position + " by 4");
Integer shift = 4;
//Update the array
for (int j = 0; j < positions.size(); j++) {
Integer checkPosition = positions.get(j);
if (checkPosition > position) {
System.out.println(checkPosition + "increased by 4");
positions.set(j,
checkPosition + shift);
}
}
}
}
//Add updated Array
map.put(key,
new HashSet<Integer>(positions));
}
}

最佳答案

最好的办法是将 HashSet 放入列表中来对其进行索引。然后您可以使用索引而不是迭代器来引用元素。只要您不删除或添加(仅更新)元素,那么您的索引就是正确的。否则你将不得不对此负责。示例:

ArrayList<Integer> positions = new ArrayList<Integer>(map.get(key));
for (int i = 0; i < positions.size(); i ++) {
// updating list
for (int j = i; i < positions.size(); j ++) {
positions.set(j, positions.get(i) + diff);
}
}

关于java - 迭代器删除/编辑是安全的,但是我还需要编辑以下所有迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19428710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com