1

Hy,

I'm a bit new to JAVA, and having a big problem. While I'm adding elements to a List<int[]> the result will be a List full of with the same value. Why JAVA is working like this?

Here is the code:

// Global variables
private static int rows = 20;
private static int columns = 30;
private static String[][] labirinth = new String[rows][columns];
private static List<int[]> walls = new ArrayList<int[]>();

// Local variables inside a function
int[] wall = new int[2];
if(row - 1 <= rows && labirinth[row-1][column] == "*")
{
    wall[0] = row-1;
    wall[1] = column;
    walls.add(wall);
}
if(row + 1 <= rows && labirinth[row+1][column] == "*")
{
    wall[0] = row+1;
    wall[1] = column;
    walls.add(wall);
}
if(column - 1 <= columns && labirinth[row][column-1] == "*")
{
    wall[0] = row;
    wall[1] = column-1;
    walls.add(wall);
}
if(column + 1 <= columns && labirinth[row][column+1] == "*")
{
    wall[0] = row;
    wall[1] = column+1;
    walls.add(wall);
}

At the end the walls variable will hold the last result of wall at multiple times.

Thanks for the help!

user2864740
  • 60,010
  • 15
  • 145
  • 220
Dávid Pörös
  • 83
  • 2
  • 11
  • 4
    Create a *new* wall object/array. Right now the code is modifying the *same* object and adding the *same* object multiple times. – user2864740 Oct 14 '14 at 08:56
  • Just FYI: Others have pointed out the problem about having to create a new object. But one thing that might interest you as well: If you actually *want* to add the same object several times but only have it in your collection once, check out the `Set` class. – blalasaadri Oct 14 '14 at 09:04
  • http://stackoverflow.com/questions/4574218/list-of-items-with-same-values , http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list?lq=1 , http://stackoverflow.com/questions/15595634/all-elements-in-linkedlist-have-same-value-as-element-added?lq=1 – user2864740 Oct 14 '14 at 09:06

2 Answers2

4

You need to know that array is an object. When you declare a variable, you are declaring a reference to that array.

That means, you are simply referring the same array and adding the same array multiple times to your walls

You should create a new array for every entry in walls

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
4

You always change the same object:

int[] wall = new int[2];

but each time before you set the new values to the wall and add it to the list you should:

wall = new int[2];

If you do not, you will just change the same wall coordinates every time and end up with just one wall which will have the coordinates that you set last.

// Local variables inside a function
int[] wall = null;
if(row - 1 <= rows && labirinth[row-1][column] == "*")
{
    wall =  new int[2];
    wall[0] = row-1;
    wall[1] = column;
    walls.add(wall);
}
if(row + 1 <= rows && labirinth[row+1][column] == "*")
{
    wall = new int[2];
    wall[0] = row+1;
    wall[1] = column;
    walls.add(wall);
}
...
Juru
  • 1,623
  • 17
  • 43