This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Customer Requirements for Developers and have Marcho Behler on-line!
See this thread for details.
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

Win a copy of Customer Requirements for Developers this week in the Jobs Discussion forum!
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
jQuery in Action, 3rd edition