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 6117487
In Process

The Archive Base Latest Questions

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

I have a problem where I retrieve and element from a list (list1), and

  • 0

I have a problem where I retrieve and element from a list (list1), and modify one of the parameters in the element and then add it to another list (list2). When I do this to the final item in list1, it will sometimes modify the parameters of the elements in list2.

This function is called once per generation, but it is not until about the 8th generation when I start to see this happen. It has affect anywhere from 2 to 16 elements in list2.

Any ideas where my screw up might be? Here’s a block of code I wrote to illustrate the problem. The problem occurs in the section where I check count==0.

    public void sampleCode (List list) {
    List differentList = new ArrayList();
    Individual element;
    Individual differentelement;
    int i = 0;
    int count = 0;
    for(i = 0; i < list.size(); i++) {

        element = (Individual) list.get(i)  ;


        // does some checking to see if this meets criteria
        // this is sorta pseudo code
        if(probability == true) { 
            element.setDoMutation(true);
            count++;
        }
        //always add this element to differentList
                    //even if no changes are made to the element
        differentList.add(i,element);
    }
        //need to make sure one elements has mutation=true;
    if(count == 0) { 
        differentelement = (Individual) list.get((list.size()-1));

        //setting this element field changes the contents of
        //different list.
        differentelement.setDoMutation(true);
        differentList.set((list.size()-1), differentelement);
    }   
}
  • 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-23T15:19:35+00:00Added an answer on May 23, 2026 at 3:19 pm

    In Java, a variable doesn’t hold an object. It holds an object reference (i.e. a pointer to an object). Getting an object fom a list and putting it in another list doesn’t make a copy of the object. Both lists simply have a pointer to the same object. So, of course, if you modify the contents of the object, both lists will have the object modified.

    Side note: You should use parameterized types (i.e. List<Individual> rather than List), and avoid declaring variables at the beginning of your methods as you would do in C. Only declare a variable when you need it. This will make the code much clearer, and reduce the scope of your variables.

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

Sidebar

Related Questions

I have a problem with getting jquery to retrieve results from a WCF service.
I have to login in to various elements to retrieve some data, problem is
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have a problem with the extra() method of queryset. So, i retrieve my
I have the following problem: Text needs to be copied from 1 wordprocessing document
I've been working on this application but have come across a problem that I
In a Kohana query, I need to retrieve only one element having the same
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

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.