0

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.

Community
  • 1
  • 1
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 2
    The first approach is fine. – shmosel Mar 14 '17 at 23:32
  • @shmosel, thanks and vote up. Why 2nd method does not work? – Lin Ma Mar 15 '17 at 19:56
  • @shmosel, thanks for referring the other question, I updated my post, but I think they referred question is not the same issue of this question I am asking. But if I read it wrong, please feel free to correct me. – Lin Ma Mar 15 '17 at 19:58
  • 1
    It's the same point. List is to ArrayList as Animal is to Dog. The inner type isn't important. – shmosel Mar 15 '17 at 20:00
  • @shmosel, I think in my case, inner matters -- it is why I think it is not the same issue. You can see first method works -- because I change something **inner**. If I read your comments wrong, please feel free to correct me. – Lin Ma Mar 15 '17 at 22:26
  • 1
    By "inner type", I meant `String`. Of course the type matters (`List` vs `ArrayList`). That's the whole point of the question I linked. – shmosel Mar 15 '17 at 22:28
  • @shmosel, thanks. Let us ignore the inner type. My question is more about why I cannot write as `List> arrOfStr = new ArrayList>();`? – Lin Ma Mar 17 '17 at 18:00
  • 1
    And the answer is in the link I posted. – shmosel Mar 17 '17 at 18:06
  • @shmosel, you mean `ArrayList` is not subclass of `List`? If so, why this part works `List<...> arrOfStr = new ArrayList<...>();` – Lin Ma Mar 18 '17 at 01:22
  • @shmosel, does it means `ArrayList` is a sub-class of `List`, but `ArrayList` is not a sub-class of `List`? – Lin Ma Mar 18 '17 at 01:23
  • 1
    No, it means `ArrayList` is not a subtype of `ArrayList`. Therefore, `ArrayList>` is not a subtype of `ArrayList>` or `List>`. – shmosel Mar 18 '17 at 01:28
  • @shmosel, understand your points and vote up, but it seems array works and doesn't array has the same issue of List? Why array works, but not List? See my new post and code here => http://stackoverflow.com/questions/42897325/different-behavior-of-array-v-s-listt-in-java-for-sub-class – Lin Ma Mar 20 '17 at 06:46

0 Answers0