In Java, it is forbidden List<Integer>a[]
to define this list array structure.
However, there are some other ways to implement a list array.
First, use node to list Wrap it up.
public class GenericArray {static class Node { public ArrayList<Integer> x; public Node() { x = new ArrayList<Integer>(); }}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(); }}}
Second, let node inherit
List<Integer>
static class nodenode extends ArrayList<Integer> {}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(); }}
Third, the use
List<List<>>
Structure
Slightly
Several alternatives to implementing list arrays in Java