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
    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 ``List``s for ``player``. – Binkan Salaryman Nov 11 '15 at 08:19

5 Answers5

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Matthew Gunn
  • 4,451
  • 1
  • 12
  • 30
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))
  • That's not correct. Java IS pass by value! When you pass in objects to a function, object references are passed by value. – Matthew Gunn Nov 11 '15 at 08:26
  • 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. – Staffan Runnsjö Nov 11 '15 at 08:37
  • 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. – Matthew Gunn Nov 11 '15 at 08:56
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.

Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38
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

Community
  • 1
  • 1
Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
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...

ETartaren
  • 170
  • 3
  • 15