aspose file tools
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
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Doubt in ArrayList's toArray function
 
Similar Threads
what does List.toArray(T[]) return?
compiler error-incompatible types
doubts in converting arraylists to array
Collection sort
My Preparation snippets (Collections!)