Can elements stored in java containers be containers?

  java, question

Seems to have tried beforeListInAddAnother oneListIs not allowed, that is to sayList<List<String>>This type is incorrect. But it seemsAddThere is no problem with an array. (List<String[]>)

First of all, likeListThe elements in this container class should beObjectCan I consider the built-in array type to be aObject? Second, why can container classes store arrays and not other containers? Because the container instance is also an object?

  1. The first thing to be clear is that containers are also objects, so containers can contain containers.
  2. Secondly, the container holds the reference of the object, not the object itself.
Java@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:

Javaprivate 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.