0

I have a class with a generic T. Then I create an ArrayList with a generic that holds T or any of its subobjects. I assign it to an ArrayList with generic T. Why can I not add to it. Here is the code snippet:

ArrayList<? extends T> list = new ArrayList<T>();
T t; //yes I know it is null currently, but this is the idea
list.add(t); //will not compile

I know it will throw a NullPointer, but that's not the issue. Why won't it let me add a T object to an ArrayList declared that it can be instantiated with an ArrayList of type T and thus instantiated with type T.

-Thanks

P.S. I did not put the code in the snippet but it is in a class with generic T defined.

JustAFellowCoder
  • 300
  • 2
  • 11
  • But what is T? You need to define it somehow? If you have a generic class, MyClass then it should work. – Joakim Danielson Apr 11 '19 at 20:16
  • @JoakimDanielson I do have that class. I put in the text that I have a generic T in a class already. This is just the snippet. – JustAFellowCoder Apr 11 '19 at 20:16
  • @nvioli Then how can I do the intended effect? – JustAFellowCoder Apr 11 '19 at 20:22
  • What is the intended effect? The code you have now is intended to be read from, not written to. – Silvio Mayolo Apr 11 '19 at 20:25
  • Per the linked answer, "Sorry, but you can't." – nvioli Apr 11 '19 at 20:25
  • Why not just use the simple ArrayList list = new ArrayList<>(); – Magnus Apr 11 '19 at 20:27
  • @SilvioMayolo the intended effect is to have an ArrayList that can take in an object of type t or a subtype. Never a super type. So for example, on runtime, it takes in some generic. Whatever that generic is, the ArrayList holds it. But that ArrayList can only take in generics that as numbers or subclasses of numbers. So it could take in doubles, but only doubles, or ints, and only ints, but never Strings. So on runtime, it takes in whatever the generic is, but the generic has to be a Number or a sub type. – JustAFellowCoder Apr 11 '19 at 20:32
  • @Magnus Cause what if I only want it to be numbers... not Strings. I wouldn't do ArrayList list = new ArrayList<>() because I want it to take any generic number, doubles, ints, etc. – JustAFellowCoder Apr 11 '19 at 20:34
  • 2
    `ArrayList` is exactly what you want. That's the whole point of subtyping. – Silvio Mayolo Apr 11 '19 at 20:35
  • @SilvioMayolo ArrayList a = new ArrayList<>() a.add(new Double(5)); will not work. – JustAFellowCoder Apr 11 '19 at 20:36
  • 1
    @JustAFellowCoder `ArrayList a = new ArrayList<>();` followed by `a.add(new Double(5));` absolutely **will** work. – Jordan Apr 11 '19 at 20:38
  • @Jordan typo, thanks. I don't see why ? extends blah is necessary then, if you can't even add anything to it. – JustAFellowCoder Apr 11 '19 at 20:42
  • @JustAFellowCoder You should read the two other questions that have been linked at the top of this question. The first one goes into a ton of detail about this exact issue, and explains in detail why you can't add anything to a list when you use ` extends X>`. – Jordan Apr 11 '19 at 20:47
  • @Jordan I did actually, it does not explain why this is necessary, I do not think. – JustAFellowCoder Apr 11 '19 at 20:50
  • It absolutely does. Especially the PECS dup, which explains both the extends and super notation. And the use case is absolutely not what you're trying to do right now. – Silvio Mayolo Apr 11 '19 at 20:52
  • Thanks guys for your time. – JustAFellowCoder Apr 11 '19 at 20:54

0 Answers0