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

  • SEARCH
  • Home
  • 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 6159697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:10:34+00:00 2026-05-23T21:10:34+00:00

I got some problem with reference assignment in java. Let me explain: in my

  • 0

I got some problem with reference assignment in java.
Let me explain: in my code, i’m using an ArrayList of BooleanWrap (a home made class of boolean) which will be later assigned as a value in a HashMap (there is a for cycle which update the values of this ArrayList until a condition is verified).
Something like this:

 ArrayList<BooleanWrap> temp=new ArrayList <BooleanWrap>(48);

 //cycle: operations to fill the ArrayList, then a condition is satisfied

 hashMap.put(index, temp);

After that, I have to reuse the temp variable, so I need to reinitialize it. I do it with the following instructions:

for(BooleanWrap bool: temp){
    bool.set(false);
  }

There comes my problem: assigning temp as a value of the hashMap will save only the reference of the variable, not the actual value.
So, reinitializing temp cause also the updating inside the hashmap (in this case, setting everything false).
Now, I think that even with clone() method I should get the same result (cause it produces a shallow copy of the ArrayList).
Is there a way to reuse the same variable temp in my cycle without assign later the reference of it to the hashmap?
Actually I can do it with a method which creates a deep copy of the arraylist:

public static ArrayList<BooleanWrap> deepcopy(ArrayList<BooleanWrap> original){

  ArrayList<BooleanWrap> copy=new ArrayList<BooleanWrap>(48);
  for(BooleanWrap bool: original)
      copy.add(new BooleanWrap (bool.get()));

  return copy;

}

but I need something done inline, without any other methods and as shorter as it can be.

Any advice/suggestion/insult?

  • 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-05-23T21:10:35+00:00Added an answer on May 23, 2026 at 9:10 pm

    After that, I have to reuse the temp variable, so I need to
    reinitialize it. I do it with the following instructions:
    for(BooleanWrap bool: temp)
    { bool.set(false); }

    That does not re-initialize the temp variable. That re-initializes the contents of your temp variable. to re-initialize the temp variable your code would look like

    ArrayList<BooleanWrap> temp=new ArrayList <BooleanWrap>(48); 
    //cycle: operations to fill the ArrayList, then a condition is satisfied 
    
    hashMap.put(index, temp);
    
    //After that, I have to reuse the temp variable, so I need to reinitialize it. 
    // I do it with the following instructions:
    
    temp = new ArrayList <BooleanWrap>(48); 
    //cycle: operations to fill the ArrayList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a sproc and some C# calling it. My problem is that both
I got some legacy code with that caption spread out as comment almost in
I got some legacy code that has this: <?PHP if(isset($_GET['pagina'])==homepage) { ?> HtmlCode1 <?php
I've got some (C#) code that relies on today's date to correctly calculate things
So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of
Has anyone got EclipseLink MOXy (I'm using eclipselink 2.1.0) to work with Java 5?
I've got a problem at programming for iOS. Already looked for some similar problems
I've got a really odd problem when building a project in VS2008. Having made
I got some problems with finding a bug in php/mysql application, and I wonder
We've got some problems with an external company trying in integrate into a WCF

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.