Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8486967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:06:02+00:00 2026-06-10T21:06:02+00:00

I got a concurrentModificatoin Exception while working over a HashSet and hence shiftedover to

  • 0

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! 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:06:04+00:00Added an answer on June 10, 2026 at 9:06 pm

    You’ve some basic misunderstanding how references and variables work in Java. In general you only create a new object when you see the new keyword like with new ConcurrentSkipListSet(). (There are some special cases how to create objects without seeing some new keyword but imho you can omit this in the first place.)

    All assignments from one variable to another variable do NOT create new objects. You assign the variable to the same object as the other. These are references to the same object.

    You have this code for example:

        while (ir.hasNext()) {
            ConcurrentSkipListSet b = new ConcurrentSkipListSet();
            b = a; // 'b' now refers to the same object as 'a'.
            ...
        }
    

    This is complete non-sense. You create a new list and drop it in the next line, because now you point the reference b to the same object the reference a is pointing at.

    That is why you effectively only use the list object you created first in all your code:

        ConcurrentSkipListSet a = new ConcurrentSkipListSet();
    

    Very much the same with your two-dimensional array. You only created one, assigned it to l and pointed g later in the code to the same array l is pointing to. After that you copy some values but in fact you copy values from and to the SAME array object. This is complete non-sense again. Push the line System.out.println(this.g[2][1]); in top of your for loops and I bet you will be surprised.

    Have a look at this SO answer which @RC. posted in his comment. It is addressing the source of your misunderstanding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Got a working AJAX form: @using (Ajax.BeginForm(...)) I want to disable the button while
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
Got the guestbook working alright. New messages are building up nicely. Now, only the
Got this error message while trying to load view: The model item passed into
Got issue My javascript array contains dates for ex. 19.4.2003,28.11.1997,3.1,,29.6,7.11.2008 (date-month-year). As you can
Got this error trying to do a new project with rails 3.2. haml issue
Got a issue with Facebook Graph API. I want to read page notifications via
Got this error while trying to get the variables out of a class. Using
I was reading this Freuqent Java concurrency problems question and got confused by an
Got a situation where we have a working production website that is moving to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.