Ich habe 2 Arraylisten:Java Arraylist <Arraylist <String>> clear() auf Kind löscht Eltern
ArrayList<ArrayList<String>> res= new ArrayList();
ArrayList<String> data= new ArrayList();
Nachdem ich resultset in Kind hinzufügen und Kind anhängen in Mutter nenne ich .clear()
auf dann data
Indizes zurückgesetzt werden.
Das Problem ist, dass .clear()
sowohl Kind als auch das Elternteil fallen lässt.
Irgendwelche Ideen, warum?
Am Ende von der gesamten Ergebnismenge bekomme ich nur die letzte Reihe 10 Mal dupliziert.
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;
}
Ausgang:
[]
[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]]
Schluss Absicht ist, die übergeordnete Liste zu analysieren und fügen Sie Daten in XWPF docx-Tabelle.
Lösung:
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);
}
Sie fügen dem Res immer dasselbe Datenobjekt hinzu, aber erstellen keine neuen Dateninstanzen. Also, wenn Sie es zu res im ersten Lauf hinzufügen, und rufen Sie clear() auf den zweiten Lauf Sie im Grunde auch res.get (1) Elemente – Nadir