I want to do 2D dynamic ArrayList example:
[1][2][3]
[4][5][6]
[7][8][9]
and i used this code:
ArrayList
If it is not necessary for the inner lists to be specifically ArrayList
s, one way of doing such initialization in Java 7 would be as follows:
ArrayList> group = new ArrayList>();
group.add(Arrays.asList(1, 2, 3));
group.add(Arrays.asList(4, 5, 6));
group.add(Arrays.asList(7, 8, 9));
for (List list : group) {
for (Integer i : list) {
System.out.print(i+" ");
}
System.out.println();
}
Demo on ideone.