This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in ArrayList Watch "Doubt in ArrayList New topic
Author

Doubt in ArrayList's toArray function

J Bolla
Greenhorn

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
Hi

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
 
Consider Paul's rocket mass heater.
 
subject: Doubt in ArrayList's toArray function
 
Similar Threads
My Preparation snippets (Collections!)
what does List.toArray(T[]) return?
Collection sort
compiler error-incompatible types
doubts in converting arraylists to array