I want to create a list of lists in Java and this is the code I am using for that.
public ArrayList<ArrayList<Object>> getRows(){
ArrayList<ArrayList<Object>> listOfRows = new ArrayList<ArrayList<Object>>();
ArrayList<Object> temporalRow = new ArrayList<Object>();
IntNode rowSentinel = newTable.sentinel;
for (int y = 0; y < this.numberOfRows; y++) {
for (int x = 0; x < this.numberOfColumns; x++) {
rowSentinel =rowSentinel.next;
Object rowElement = rowSentinel.arrayCol.get(y);
temporalRow.add(rowElement);
}
rowSentinel = newTable.sentinel;
listOfRows.add(temporalRow);
System.out.print(temporalRow);
System.out.println(Arrays.toString(listOfRows.toArray()));
temporalRow.clear();
}
return listOfRows;
}
However, when I see the final list of lists the console gives me the following output:
[oddy1, minino1, libertad1][[oddy1, minino1, libertad1]]
[oddy2, minino2, libertad2][[oddy2, minino2, libertad2], [oddy2, minino2, libertad2]]
[oddy3, minino3, libertad3][[oddy3, minino3, libertad3], [oddy3, minino3, libertad3], [oddy3, minino3, libertad3]]
[[], [], []]
I also tried defining listOfRows
as an array of Objects
, Object[]
, with size the number of rows, and adding elements by doing Object[0]
,Object[1]
, etc. but I ended up getting the same result.
I wanted to get a final list of the form
[[oddy1, minino1, libertad1],[oddy2, minino2, libertad2],[oddy3, minino3, libertad3]]
I really don't see what I'm doing wrong with the code. Could anyone help me with this?