Java中ArrayList的ArrayList
我们已经讨论过 ArrayList 的数组是不可能没有警告的。一个更好的主意是使用 ArrayList 的 ArrayList。
// Java code to demonstrate the concept of
// array of ArrayList
import java.util.*;
public class Arraylist {
public static void main(String[] args)
{
int n = 3;
// Here aList is an ArrayList of ArrayLists
ArrayList > aList =
new ArrayList >(n);
// Create n lists one by one and append to the
// master list (ArrayList of ArrayList)
ArrayList a1 = new ArrayList();
a1.add(1);
a1.add(2);
aList.add(a1);
ArrayList a2 = new ArrayList();
a2.add(5);
aList.add(a2);
ArrayList a3 = new ArrayList();
a3.add(10);
a3.add(20);
a3.add(30);
aList.add(a3);
for (int i = 0; i < aList.size(); i++) {
for (int j = 0; j < aList.get(i).size(); j++) {
System.out.print(aList.get(i).get(j) + " ");
}
System.out.println();
}
}
}
输出:
1 2
5
10 20 30