Suppose you have an ArrayList of MimeMessages:ArrayList<MimeMessage> messages = new ArrayList<MimeMessage>(20);
You add some messages to it...messages.add(thisMsg);
...and then the method you're coding up has to return a list of messages. This has to be a MimeMessage array, and you can't change that due to hysterical porpoises.
So how do you do this?
Well, hey, this should be easy, right? ArrayList has a toArray()
method. So just...return messages.toArray();
...and Bob's your uncle. Right?
HAHAHAHAHAHAHA, YOU STUPID, DELUDED FOOL! toArray()
returns an array of Object
, not an array of what you created the ArrayList
as! I mean, what were you thinking - expecting a collection of type Foo
to convert into an array of Foo
s? That's just crazy talk!
No, Java doesn't care that your ArrayList
was template instantiated on MimeMessage
, thus ensuring beyond any possible doubt - at compile time
- that every item in message
! And no, Java doesn't care that it has Run-Time Type Identification
and thus also knows damn well at run-time exactly
what type of objects are in your ArrayList
! But no, in spite of all that, .toArray()
still will not return a list of MimeMessage
Now... just GUESS which of the following syntaxes you must use to make this stupid little bullshit operation actually work the way it should:( Do you give up yet?Collapse )