0

I tried to add an object of the class Foo to a List<? extends Foo>, but the code does not compile.

final Foo[] array = new Foo[] {new Foo()};
final List<? extends Foo> list = new ArrayList<>(array.length);
for (final Foo foo : array)
{
     // The List<? extends Foo> does not accept the foo object.
     list.add(foo);
}

Here is the question again: why is that the case? That doesn't make any sense, does it? I just want a List that can contain objects who extend the Foo class.

  • 1
    Not clear what you want to do here. What is the context of this code, what are you trying to achieve? – user3237736 Apr 08 '18 at 20:41
  • 4
    This example is neither complete nor verifiable, so there's not much we can do for you. Please provide a [minimal, complete, and verifiable example](https://stackoverflow.com/help/mcve) of your problem. – Silvio Mayolo Apr 08 '18 at 20:42
  • Alright. Now, what am I supposed to do? –  Apr 08 '18 at 20:42
  • "I just want to know how to fix some annoying behaviour" - Which is...? – Jacob G. Apr 08 '18 at 20:47
  • 2
    @ToxicTV You haven't actually asked a question. – SeverityOne Apr 08 '18 at 20:48
  • 1
    I still can't get this to run or produce an error similar to what you describe. We're being left to guess what `Event` is, which is probably a decent chunk of the problem. – Silvio Mayolo Apr 08 '18 at 20:49
  • Alright, something else you need? –  Apr 08 '18 at 20:51
  • Possibly see https://stackoverflow.com/q/2723397/2891664 – Radiodef Apr 08 '18 at 20:51
  • getEventQueue signature? – Beri Apr 08 '18 at 20:52
  • It is not possible to add to a List extends SomeClass>. I’m not entirely sure but you might need a generic method. – cpp beginner Apr 08 '18 at 20:53
  • The problem is that you can't add objects to a `List extends Blah>`, so people are asking for an MCVE because we don't know how you should change your code. All we can say is "this doesn't work", which you already know. – Radiodef Apr 08 '18 at 20:53
  • 3
    @Toxic *surprised how quickly someone downvotes a question*? Quickly downvoting is one of the *strengths* of this community in order to keep the quality (clear, answerable, future usefulness, etc) of questions high. – Bohemian Apr 08 '18 at 20:58
  • Well, I guess your right about that. I just can't stand critique. –  Apr 08 '18 at 21:02
  • @Bohemian The problem that I just have with down voting is simple: you edit a question, but people already are unable to find it, because it has been down voted and you can only post a new question every 24 hours. This sucks. –  Apr 08 '18 at 21:10
  • I get downvoted questions in my feed all the time. Personally, I check them out a lot to see if I want to place a close vote on it. If I decide that it doesn't need to be closed, I'll often stick around and help the asker. So I don't think downvotes preclude you from getting answers. – Silvio Mayolo Apr 08 '18 at 21:18
  • And there is a 24 hours threshold, because... well, uhm. Maybe, because you want to make my life harder than it already is? I know the answer to this question. It's called spam. ;) –  Apr 08 '18 at 21:41

1 Answers1

4

I think you're misunderstanding what List<? extends Event> actually means. It doesn't mean "I can add any subclass of Event to this list". That's simply what a List<Event> is, since subclasses can be upcast to Event. List<? extends Event> says "there is some subclass of Event which restricts this type". So if foo has type List<? extends Event>, then foo could feasibly be a List<Event>, but it could also be a List<SomeSpecificTypeOfEvent>, in which case you aren't allowed to add things that aren't SomeSpecificTypeOfEvent to it. I think what you want is simply List<Event>; the wildcard is overkill here and is not necessary.

Further reading on variance annotations (extends and super): https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)

Edit: As Johannes Kuhn pointed out in the comments, if all you're doing is adding elements to the list, then the most general type you can have is not List<Event> but List<? super Event>, since you could feasibly pass a list of any supertype of Event and your method would still work correctly. Some care must be taken, as using ? super Event could cause some issues trying to access list elements later, but if all you're doing in the method is writing to the list, then it works perfectly.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 1
    Actually, he wants `List super Foo>`, so a `List` would be fine too. – Johannes Kuhn Apr 08 '18 at 21:13
  • @JohannesKuhn I edited the question just a minute ago to be more precise about my problem. Thus his answer may seem too complex at first. –  Apr 08 '18 at 21:16