1

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?

shoover
  • 3,071
  • 1
  • 29
  • 40
user284639
  • 53
  • 4
  • possible duplicate with: [http://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java](http://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java) – Mouaici_Med Mar 08 '17 at 17:51
  • Try using a debugger , it will help out in finding the logical error. – Amit Kumar Mar 08 '17 at 19:44
  • I'm trying to do that, but I don't see it. I think for some reason whenever I use add on listOfRows, it is adding whatever temporalRow is refering to at that point in the program. I really don't know what's going on. – user284639 Mar 08 '17 at 19:48
  • step 1 is learn java. step 2 is pay attention. If you store a reference to a list then call list.clear() on that reference, the stored reference (which points to the same object) is also cleared. – DwB Mar 08 '17 at 20:08
  • Thanks. I am trying to learn Java, that's why I'm asking and trying to find a solution to my problem. I don't see the need to be rude with your steps. – user284639 Mar 08 '17 at 20:11

2 Answers2

0

Replace temporalRow.clear() with temporalRow = new ArrayList<Object>(), that should fix it. Right now, your listOfRows contains three times the reference to the same object, and the elements you added previously always get deleted by temporalRow.clear().

Jakob
  • 1,156
  • 3
  • 15
  • 26
0

Check Out the code below

ArrayList<Test> temporalRow = new ArrayList<Test>();

    Test t = new Test();
    t.i = 5;
    temporalRow.add(t);
    System.out.println(temporalRow.get(0).i);
    t.i = 6;
    System.out.println(temporalRow.get(0).i);

    class Test {
public int i;
}

The output of above code is 5 and 6 because temporalRow list contains the reference to object t and if that object is modified somewhere else, the changes are, obviously, reflected in temporalRow. Your case is similar. (Hint : temporalRow.clear();) I think maybe you would like to familiarize yourself with the concept of deep copy vs shallow copy.

opensam
  • 368
  • 1
  • 4
  • 10