user6569270
user6569270

Reputation:

Java ArrayList<ArrayList<String>> clear() on child clears parent

I have 2 arraylists:

ArrayList<ArrayList<String>> res= new ArrayList();
ArrayList<String> data= new ArrayList();

After I add resultset into child and append child into parent I call .clear() on then data to reset indexes.

The problem is that .clear() drops both child and the parent.

Any ideas to why?

In the end from the entire resultset I just get final row duplicated 10 times.

public ArrayList<ArrayList<String>> getTemplateTableData() {
    ArrayList<ArrayList<String>> res = new ArrayList();
    ArrayList<String> data = new ArrayList();
    CDb cb = new CDb();
    OracleConnection conn = cb.getConn();
    OraclePreparedStatement ps = null;
    OracleResultSet rs = null;
    OracleResultSetMetaData rsm = null;
    try {
      ps = (OraclePreparedStatement)
              conn.prepareStatement("Select * From Va_User_Infos_V t Where t.User_Id = 1");
      ps.execute();
      rs = (OracleResultSet)ps.getResultSet();
      rsm = (OracleResultSetMetaData)rs.getMetaData();
      while(rs.next()) {
        int col = 2;
        data.clear();
        System.out.println(data);
        while(col < rsm.getColumnCount()) {
          col++;
          data.add(rs.getString(col));
        }
        res.add(data);
        System.out.println(data);
      }
      System.out.println(res);
    } catch(Exception e) {
      e.printStackTrace();
    } finally {
      cb.done(rs);
      cb.done(ps);
      cb.done(conn);
    }
    return res;
  }

Output:

[]
[1Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[2Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[3Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[4Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[5Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[6Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[7Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[8Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[9Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[]
[10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]
[[10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8], 
 [10Col-1, Col-2, Col-3, Col-4, Col-5, Col-6, Col-7, Col-8]]

Final intention is to parse the parent list and add data into XWPF docx table.

Solution:

  while(rs.next()) {
    int col = 2;
    data.clear();
    while(col < rsm.getColumnCount()) {
      col++;
      data.add(rs.getString(col));
    }
    ArrayList clone = (ArrayList)data.clone();
    res.add(clone);
  }

Upvotes: 2

Views: 294

Answers (1)

GhostCat
GhostCat

Reputation: 140427

What you are doing: putting a reference to the child list into the parent list.

You are not creating a copy of the child list. Just putting references to the same list in different places.

Surprise: when you now modify that child list (no matter what you do: adding, removing, clearing, sorting ...) all other references will show you the changed content!

Two solutions:

  • create new child lists; and do not modify a list after adding it to the parent list
  • "flatten" your parent list (make it a List<String> too); and use addAll() to add the contents of the child list!

Upvotes: 5

Related Questions