Seems to have tried before
ListIs not allowed, that is to say
List<List<String>>This type is incorrect. But it seems
AddThere is no problem with an array. (
First of all, like
ListThe elements in this container class should be
ObjectCan I consider the built-in array type to be a
Object? Second, why can container classes store arrays and not other containers? Because the container instance is also an object?
- The first thing to be clear is that containers are also objects, so containers can contain containers.
- Secondly, the container holds the reference of the object, not the object itself.
@Test public void testList() bracket List<List<String>> lists = new ArrayList<List<String>>(); List<String> str = new ArrayList<String>(); str.add("hello world"); lists.add(str); System.out.println(lists.get(0).get(0)); bracket
Look at the source code of ArrayList:
private transient Object elementData;
ArrayList actually contains an Object array, but ArrayList is canonical. Therefore, whenever you take it out, you will force it to convert to the original type.