gpt4 book ai didi

java - Java Set中的并发修改异常

转载 作者:行者123 更新时间:2023-11-29 03:23:31 24 4
gpt4 key购买 nike

作为我程序的一部分,我坚持并发修改异常。这是提到的部分:

PriorityQueue<Customer> marginalGainHeap = new PriorityQueue<Customer>(
1, new Comparator<Customer>() {
public int compare(Customer c1, Customer c2) {
return Double.compare(c1.getMarginalGain(),
c2.getMarginalGain());
}
});

// set of all remains available nodes
availableNodes.removeAll(churnNet);
for (Customer avail : availableNodes) {
avail.setMarginalGain(0);
marginalGainHeap.add(avail);
}
while (seedSet.size() <= budget) {
**for (Customer remainingNode : availableNodes) {**

remainingNode.setMarginalGain(calculateMarginalGain(
remainingNode, seedSet, network, availableNodes,
churnNet));

marginalGainHeap.remove(remainingNode);
marginalGainHeap.add(remainingNode);
}
seedSet.add(marginalGainHeap.poll());
}

这里是 calculateMarginalGain 方法:

private int calculateMarginalGain(Customer remainingNode,
HashSet<Customer> seedSet,
DirectedSparseGraph<Customer, Transaction> net,
Set<Customer> availableNodes, HashSet<Customer> churnNetwork) {
// Marginal gain for short-term campaign
HashSet<Customer> tmp = new HashSet<Customer>(); // seedset U
// {remainingNode}
tmp.add(remainingNode);
Set<Customer> tmpAvailableNodes = availableNodes;
HashSet<Customer> NeighborOfChurn = getNeighbors(churnNetwork, net);
// sigma function for calculating the expected number of influenced
// customers- seedSettmp=seedset U {u}
tmpAvailableNodes.removeAll(NeighborOfChurn);
Set<Customer> influencedNet = getNeighbors(tmp, net);
tmpAvailableNodes.retainAll(influencedNet);
return tmpAvailableNodes.size();
}

我在用 ** 指定的程序行中遇到了这个异常。我发现这个错误可能是由迭代器引起的。但是我一个都没用!请帮我找出导致该异常的原因以及我该如何解决?

问候。

最佳答案

将您的设置转换为数组可能会解决您的问题

例子:

Set<String> set = new Set<String>(size);
String[] array = set.toArray(new String[set.size()]);

所以在你的 for 循环中你可以做类似的事情:

for(String foo : set.toArray(new String[notis.size()])) {
// Loop stuff here
}

关于java - Java Set中的并发修改异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22308654/

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