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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:41:22+00:00 2026-06-04T06:41:22+00:00

I am having trouble understanding why my code is behaving as it does. What

  • 0

I am having trouble understanding why my code is behaving as it does. What I do is create an ArrayList of a custom class. Populate the class object then add it to the ArrayList. I then reuse the class object and populate it with new values and add it to the ArrayList EXCEPT the first one gets overwritten by the second?? Is there some sort of internal pointer or the arraylist isn’t yet committed that could cause this?

Here I declare my ArrayList

private ArrayList<LiftData> arrylst_LiftData = new ArrayList<LiftData>();

Then I create my custom data object (just a class with some properties)

LiftData obj_LData = new LiftData();

Some code not shown populates it with valid data and I add it to the ArrayList

arrylst_LiftData.add(obj_LData);

Now at this point the code goes throught the loop populating the obj_LData with new values and then it gets added to the arraylist but the first one is gone???

If I add the second line shown under the ArrayList add it all works???

arrylst_LiftData.add(obj_LData);
obj_LData = new LiftData();

Can someone please edjumakate me on what is going on?

  • 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-04T06:41:24+00:00Added an answer on June 4, 2026 at 6:41 am

    An ArrayList, like all Java collections — indeed, all variables in Java — contains references to objects, not the objects themselves. It sounds like you’ve only ever created a single LiftData object, and are filling the ArrayList with multiple references to it. Each time you change its data, every object in the ArrayList will change — because they’re all the same object.

    This is in contrast to what you’d get if you used essentially the same syntax in C++; inserting into a vector is going to create a copy of the object, and you could indeed modify the object, re-add it, and get two different objects in the vector. But Java doesn’t work that way: all variables of object type are essentially pointers.

    In your loop that populates the list, you need to create a new LiftData object each time around.

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

Sidebar

Related Questions

I'm having trouble understanding following code: if (IsPostBack) { int CheckBoxIndex; ArrayList CheckBoxArray; bool
I am having trouble understanding why NSLog reports dog when the code is run.
I'm having some trouble understanding a bit of code. I've got 2 classes Company
Im having trouble understanding class relationships after being asked to research it further, would
I am having trouble understanding this code: $.functionone = function(){ function setOptions(newOptions){ ... }
I'm having trouble understanding why my code will not work asynchronously. When running asynchronously
I was going through the source code of jQuery. I'm having trouble understanding the
from here Im having trouble understanding they say that ado.net does not include a
Im having trouble understanding why the following code displays The First Key = (null)
I'm having trouble understanding some web part communication code, and how to make it

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.