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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:41:17+00:00 2026-05-30T05:41:17+00:00

I have a 2D ArrayList . The ArrayList contains 10 ArrayList s. I tried

  • 0

I have a 2D ArrayList. The ArrayList contains 10 ArrayLists. I tried the following code:

This is the main 2D ArrayList. Inside the main ArrayList there are10 ArrayLists:

 ArrayList<ArrayList<Items>> arrayList = new ArrayList<ArrayList<Items>>();

Here I tried to create a copy of one of the ArrayList (selectedRow is just a number which says which ArrayList I get)

ArrayList<Items> newList = new ArrayList<Items>(arrayList.get(selectedRow));

After that I create another ArrayList:

   ArrayList<Items> changeList = new ArrayList<Items>(it.returnTheNewArrayList(newList,randomItem));

Then in another class I created this method. The purpose of this method is to change an attribute of one of the objects.

public  ArrayList<Items> returnTheNewArrayList(ArrayList<Items> a,int item){

  int randomBin = r.nextInt(50);

    for(Items i:a){

        if(item==i.itemIds()){

            while(randomBin==i.bins()){
                randomBin = r.nextInt(50);

            }
            i.setBin(randomBin);

        }

    }
return a;

}

Finally, I set the new ArrayList in the 2D ArrayList

arrayList.set(whichList, changeList);

This is the procedure. I have find out, that when I run this procedure, both newList and changeList are the same. In both of these ArrayLists I’m saving the change I did in the method returnTheNewArrayList (I found out it with the debugging). But I want to change only one (the changeList).

What did I do 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-30T05:41:18+00:00Added an answer on May 30, 2026 at 5:41 am

    The lists contain references to objects. When you call i.setBin(...) that’s (presumably) making a change to the object itself.

    Each list has an independent copy of the references – so you could remove an element from one list without affecting the other – but they’re only references.

    Imagine you gave two people clipboards, with the same list of home addresses on. One person went and painted the front door of every house list on their clipboard red, then the second person visited all the same houses. The second person would see red doors, wouldn’t they? It’s the same thing here. The lists contain references, not objects.

    If you want the lists to be completely independent, you’ll need to populate them with references to different objects.

    EDIT: I’ve just noticed that you’ll also need to change your returnTheNewArrayList method, which actually isn’t even creating a new ArrayList in the first place!

    public ArrayList<Items> returnTheNewArrayList(ArrayList<Items> a,int item) {
        // Stuff which doesn't change the value of a...
    
        return a;
    }
    

    Again, the value of a is just a reference to the list… so when you return the same reference back, you’re not returning a new ArrayList at all.

    You really need to understand how references and objects work in Java – it’s absolutely crucial to working with the language.

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

Sidebar

Related Questions

I have an arraylist that contains items called Room. Each Room has a roomtype
This is my code in ASP.Net C# ArrayList myArrayList = new ArrayList(); myArrayList.Add(Apple); myArrayList.Add(Banana);
I have Two Arraylist RunningProcessList AllProcessList its contains following values are RunningProcessList: Receiver.jar AllProcessList:
I have an ArrayList that contains Strings and also contains other ArrayLists which may
I have a couple of ArrayLists with variable length and sometimes null. This ArrayList
I have a class Matrix which contains ArrayList<ArrayList<Double>> matrix; inside, and I want to
I have an Arraylist of HashMap . Each HashMap element contains two columns: column
I have an ArrayList called account which contains Strings. I'm trying to write a
I have two arraylists, one contains Strings and the other contains Classes that have
I have a class that contains two arraylists which I'm trying to store objects

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.