在Java中,禁止定义Lista[],这种List数组结构。 但是还是可以使用其它一些方式来实现列表数组。
#一、使用Node把List包裹起来
public class GenericArray {
static class Node {
public ArrayList x;
public Node() {
x = new ArrayList();
}
}
public static void main(String[] args) {
Node[] a = new Node[10];
for (int i = 0; i < a.length; i++) {
a[i] = new Node();
for (int j = 0; j < i; j++) {
a[i].x.add(j);
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].x.size(); j++) {
System.out.print(a[i].x.get(j));
}
System.out.println();
}
}
}
二、让Node继承List
static class nodenode extends ArrayList {
}
public static void main(String[] args) {
nodenode[] a = new nodenode[10];
for (int i = 0; i < 10; i++) {
a[i] = new nodenode();
for (int j = 0; j < i; j++) {
a[i].add(j);
}
}
for (int i = 0; i < a.length; i++) {
for (Integer j : a[i]) {
System.out.print(j + " ");
}
System.out.println();
}
}
三、使用List>结构
略