0

Why my block isn't work well in ArrayList?

My problem is that: I have a class what contains two variable: a String and a two dimensions block (int[] []). I have an ArrayList what contains these classes. But because of some reason what I don't know all my two dimensions block are the same. I read the blocks from a file and in an other example it worked very well. My question is there is any special thing what I have to know when I use blocks in ArrayList elements?

14th Mar 2017, 3:10 PM
Cselyuszka Balázs
Cselyuszka Balázs - avatar
2 Respostas
+ 6
tomb = new int[10][10]; tomb = feladat; This is the wrong part of your code. In the first row you create a new two-dimensional array but in the next row you drop it and set tomb reference to feladat. And you use this feladat array for every megoldas objects, that's why they all reference to the same array, which contains the last read data. Two solutions: - create new array for every megoldas and remove the first row - copy content of feladat into the tomb array (not just change reference) I suggest you the first solution.
14th Mar 2017, 4:58 PM
Tamás Barta
Tamás Barta - avatar
0
https://code.sololearn.com/cNApT11jv3MR/#java This is the place of my code. The beginning of the megoldas.txt is: 10 Absolon 0 0 0 0 11 0 11 0 0 0 11 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 11 0 0 0 0 0 11 0 0 0 0 0 0 0 3 0 0 0 0 0 0 11 0 0 0 0 0 2 0 11 0 0 0 0 11 0 0 0 0 0 0 3 0 0 0 0 0 0 0 3 0 0 0 0 11 0 3 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0 Agaton 0 0 0 0 11 0 11 0 0 11 11 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 11 0 0 0 0 0 11 0 0 0 0 0 0 0 3 0 0 0 0 0 0 11 0 0 0 0 0 2 0 11 0 0 0 0 11 0 0 0 0 0 0 3 0 0 0 0 0 0 0 3 0 0 0 0 11 0 3 0 0 0 0 11 0 0 0 0 0 0 0 11 0 0 0
14th Mar 2017, 3:18 PM
Cselyuszka Balázs
Cselyuszka Balázs - avatar