This is not as much a problem with the inner list as it is the problem of the generic parameter. As long as the generic parameter is the same (i.e. ArrayList<Object>
) the initialization succeeds. Otherwise, Java does not let you make an assignment, because the compiler cannot guarantee type safety.
Imagine that this is possible:
// Let's pretend this compiles:
List<List<Object>> a = new ArrayList<ArrayList<Object>>();
Now the following must be valid:
a.add(new LinkedList<Object>());
However, this is invalid, because the object is an ArrayList
of ArrayList
s, so inserting a linked list into it is clearly invalid.
You can do this, however:
List<List<Object>> a = new ArrayList<List<Object>>();
This should be good enough to program to the interface.