我在处理 HashSet 时遇到了 concurrentModificatoin 异常,因此转移到了 ConcurrentSkipListSet。虽然并发问题解决了,但我现在有一个新问题。ConcurrentSkipListSet 似乎默认是静态的。是这样吗?我在一个递归函数中使用它,该函数有一个迭代器,它迭代相同。当下一个元素被迭代时,由于同一函数的另一个重复实例所做的更改会反映在同一个对象中(在正常递归中通常不会出现这种情况,其中每个实例都在堆栈中分配了自己的空间)。而且我不希望这些更改被反映。这个问题有什么解决办法吗?
提前致谢 :)
我正在使用java,这是代码......
public class TSA {
int[][] l = { { 0, 1, 30, 65535 }, { 50, 0, 15, 5 }, { 65535, 65535, 0, 15 }, { 15, 65535, 5, 0 } };
int[][] g;
TSA() {
int n = 4;
this.g = this.l;
int k = 0;
ConcurrentSkipListSet a = new ConcurrentSkipListSet();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
this.g[i][j] = this.l[i][j];
}
}
System.out.println(this.g[2][1]);
a.add(2);
a.add(3);
a.add(4);
Iterator ir = a.iterator();
int[] path = new int[n];
k = 0;
while (ir.hasNext()) {
ConcurrentSkipListSet b = new ConcurrentSkipListSet();
b = a;
b.add(2);
b.add(3);
b.add(4);
int next = (Integer) ir.next();
System.out.println("next in main is " + next);
System.out.println("called with " + b);
path[k++] = this.l[0][next - 1] + gfun(next, b);
System.out.println("min path is..." + path[k - 1]);
}
}
public static void main(final String[] args) {
new TSA();
}
int gfun(final int next, final ConcurrentSkipListSet d) {
ConcurrentSkipListSet b = d;
if (b.size() != 1 && b.size() != 2) {
b.remove(next);
System.out.println(b);
int[] path = new int[b.size()];
int k = 0;
Iterator ir = b.iterator();
while (ir.hasNext()) {
int a = (Integer) ir.next();
System.out.println(a + " iterator prob " + b);
path[k] = this.l[next - 1][a - 1] + gfun(a, b);
System.out.println("path[" + k + "] is" + path[k]);
k = k + 1;
}
return min(path);
}
else if (b.size() == 2) {
System.out.println("second instance..." + next + ".." + b);
Iterator irrr = b.iterator();
int a = (Integer) irrr.next();
b.remove(next);
return (gfun(next, b));
}
else {
Iterator irr = b.iterator();
int j = (Integer) irr.next();
System.out.println("inside prog size is 1" + b);
System.out.println("l[" + next + "][" + j + "] is " + this.l[next - 1][j - 1]);
int ans = this.l[next - 1][j - 1] + this.g[j - 1][0];
System.out.println("l[" + next + "][" + j + "]+g[" + j + "][1] which is " + ans + " is r returned");
return (ans);
}
}
private int min(final int[] path) {
int m = path[0];
for (int i = 0; i < path.length; i++) {
if (path[i] < m) {
m = path[i];
}
}
return m;
}
}
我正在从集合 b 中删除元素,当函数返回到以前的状态(while 循环内的迭代器)时,原始集合 b 会受到影响。我不希望这种情况发生。有人请帮忙!:(