3

please help meto add integer in the ArrayList inside an ArrayList.. Here is the code..

ArrayList<ArrayList<Integer>> player = new ArrayList<ArrayList<Integer>>(10);
ArrayList<Integer> array = new ArrayList<Integer>(10);
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);

player.add(array);
player.add(array);

If i check what's inside array and player using debug..

array[1,2,3,4,5]
player[[1,2,3,4,5],[1,2,3,4,5]]

Now, i want to add Integer on player's ArrayList slot 0 using this:

player.get(0).add(6);

but instead of getting of this:

player[[1,2,3,4,5,6],[1,2,3,4,5]]

i got this:

player[[1,2,3,4,5,6],[1,2,3,4,5,6]]

In short, player's ArrayLost slot 0 and slot 1 received the integer that i'ved add..

Please help.. Thanks in advance.. :)

1
  • 1
    You are adding the same List of String twice. As the List data type is by reference all changes done to array affects player.get(0) and player.get(1). To solve this, you need to create two distinct Lists for player. Commented Nov 11, 2015 at 8:19

5 Answers 5

3

You added the same ArrayList to both entries of the player ArrayList. Both entries of the player ArrayList are exactly the same object.

You should make a second ArrayList:

ArrayList<Integer> array2 = new ArrayList<Integer>();

Then add it as the second item of the player ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

Hint: To make a copy of an ArrayList, you can use the copy constructor ArrayList#<init>(Collection)
Thank you guys.. i will check your anwers.. :)
0
ArrayList<ArrayList<Integer>> player = new ArrayList<ArrayList<Integer>>(10);
ArrayList<Integer> array = new ArrayList<Integer>(10);
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.add(5);
ArrayList<Integer> array1 = new ArrayList<Integer>(array);
player.add(array);
player.add(array1);

Already answered...

Comments

0

As others mentioned you actually add the same element to both lists.

If you do not want to fill both ArrayLists with the initial values. You can keep your code and only change the following lines:

player.add(array);
player.add(array);

to:

player.add(array);
player.add(new ArrayList(array)); // alternatively player.add(array.clone());

This makes a new ArrayList to the second entry in players.

Comments

0

The problem with your code is that - when you add the array to player object you pass the same reference of array object.

When you do this player.get(0) you are getting the same object which is ArrayList<Integer> array = new ArrayList<Integer>(10); and then when you do the adding player.get(0).add(6); you are pointing to the reference of array object and java passes objects by reference. see this SO thread

Comments

0

That is because adding array twice to player does not add the values of array to player twice, it only adds two copies of the reference to array to player

player[[1,2,3,4,5],[1,2,3,4,5]]

is really

player[array,array]

You can test this with for example jUnits assertTrue:

assertTrue(player.get(0) == player.get(1))

3 Comments

That's not correct. Java IS pass by value! When you pass in objects to a function, object references are passed by value.
In a way, yes, and in another way, no. It passes the value of the reference to the object, which is what you write, so yes. But it passes the reference to the object, which is what you are (usually) interested in with the code. That is why pass-by-reference is a convenient way of thinking, although it is technically false.
I would avoid the whole pass by reference vs. pass by value distinction when talking with a Java beginner. It's an important distinction for C, Fortran, etc.. but it's likely just to blow completely over a beginner's head. I'd emphasize that Object variables in JAVA don't hold the literal objects themselves but pointers to the object. That's a clear way of thinking about the mistake here. It also has the benefit of being technically correct rather than technically false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.