How can I use the same arraylist row to add values into the nested array tableValues since the clear command removes the data in row.
Thanks
ArrayList<ArrayList<String>> tableValues = new ArrayList<ArrayList<String>>();
ArrayList<String> row = new ArrayList<String>();
row.add("a");
row.add("b");
row.add("c");
tableValues.add(row);
row.clear();
row.add("d");
row.add("e");
row.add("f");
tableValues.add(row);
row.clear();
row.add("g");
row.add("h");
row.add("i");
tableValues.add(row);
row.clear();
System.out.println(tableValues);
I’m sure you really want to add three different ArrayLists, so change each
row.clear()to:Why would you want to “use the same arraylist row to add values”? I can’t see why you’d want your top-level
ArrayListto basically contain the same reference three times. Why would you not want them to be references to independent lists?