It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in ArrayList's toArray function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in ArrayList Watch "Doubt in ArrayList New topic

Doubt in ArrayList's toArray function

J Bolla

Joined: Nov 23, 2004
Posts: 17

Both the toArray functions of the ArrayList return Object[] but the commented
line gives a runtime exception. We know that arrays cant be upcasted like the
regular datatypes.

This :

is identical to

So it seems that the assignment and casting is redundant in the
first statement.
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125

toArray( RunTimeType[] ), RunTimetype type might be any type which can be cast for the arrayList elements.

In ur case toArray(Integer[])it converts the arrayList elements into Integer Array elements.

But Object[] toArray() returns Object Array, so you can't downcast the SuperType to SubType

More please read, collection's toArray(Object[]) and toArray().
I hope you get me.

Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
I agree. Here's the link:
subject: Doubt in ArrayList's toArray function
It's not a secret anymore!