I got a concurrentModificatoin Exception while working over a HashSet and hence shiftedover to ConcurrentSkipListSet. Although the concurrency issue is solved, i now have a new problem. It seems that a ConcurrentSkipListSet is static by default.Is it so? I'm using it in a recursive function which has an iterator that iterates over the same. When the next element is iterated, the changes made due to another instance of recurrence of the same function is reflected in the same object ( which is not usually the case in normal recursion wherein each instance is allocated its own space in a stack ). And i don't want the changes to be reflected. Is there any solution to this problem?
Thanks in advance :)
I'm using java and here's the code...
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;
}
}
I'm removing the elements from set b and when the function returns to a previous state( iterator inside the while loop) the original set b is affected. I don't want this to happen. Somebody please help! :(