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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:01:30+00:00 2026-05-26T06:01:30+00:00

As the title says, I have two ArrayLists. Strangely, setting a value on one

  • 0

As the title says, I have two ArrayLists. Strangely, setting a value on one arraylist changes that of another.

Some information: these are ArrayLists of type Entry, each of which holds an amount and a value (which is what you see in the parentheses). I’m trying to turn this array:
[(5,2)(2,5)(3,2)]

into these [(1,2)] and [(4,2)(2,5)(3,2)]. i == 1 in this case, and remainingAmt == 1.

ArrayList<Entry> firstHalf = new ArrayList<Entry>();
    ArrayList<Entry> secondHalf = new ArrayList<Entry>();

    for(int j = 0; j<=i;j++){
        firstHalf.add(rleAL.get(j));
    }
    for(int k = i; k<rleAL.size(); k++){
        secondHalf.add(rleAL.get(k));
    }
    System.out.println(firstHalf);//gives (5,2)
    firstHalf.get(i).setAmount(remainingAmt);
    System.out.println(firstHalf);//gives (1,2) *CORRECT*
    secondHalf.get(0).setAmount(rleAL.get(i).getAmount() - remainingAmt);
    System.out.println(firstHalf);//gives (0,2) *WRONG*
  • 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-26T06:01:30+00:00Added an answer on May 26, 2026 at 6:01 am

    Nothing unusual there–if the underlying objects are shared between lists, a change in one object will be reflected in the other. If you need the lists to be independent, you need to use different objects in each list by either creating new objects from scratch, or copying the initial object to a new object in list #2.

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

Sidebar

Related Questions

Pretty much what the title says really. We have some code that is .NET
Basically what the title says... I need to have an image that when clicked,
Ok, so as the title says, I have an HTML page that I fetch
As the title says, I have a method that throws a NullReferenceException in my
As the title says, I have a hidden border with some controls inside, and
As the title says... I have a route (the first one listed) which looks
Well as the title says i have 2 web projects that have the same
The title says it mostly. If I have an object in Python and want
The title says enough I think. I have a full quality BufferedImage and I
OK, I don't think the title says it right... but here goes: I have

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.