If I want to create a list of list of Strings in Java, for example here is a logical representation (for what I mean a list of list of Strings), and I find the 2nd method below does not compile pass? I am confused since I think ArrayList is an implementation of List interface, what is wrong with 2nd method?
Also want to consult expert here what is the right way to create a list of list of Strings (if I want to use List
interface type on variable type other than using ArrayList
concrete implementation type)?
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Source code in Java,
List<List<String>> arrOfStr = new ArrayList<List<String>>(); // first method works
List<List<String>> arrOfStr = new ArrayList<ArrayList<String>>(); // second method does not work -- it cannot compile
Edit,
I think my question is different from this question (Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?) as I am asking container of container case, but if I read it wrong, please feel free to correct me.
> arrOfStr = new ArrayList>();`?
– Lin Ma Mar 17 '17 at 18:00>` or `List
>`.