• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in ArrayList's toArray function

 
J Bolla
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic