aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Casting issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Casting issue" Watch "Casting issue" New topic
Author

Casting issue

Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Hey Guys,
I am building an ArrayList of DataInfo objects.
Then, when I return I want to cast it back to an array of DataInfo objects.
So this sample code (very rough):

Now this compiles fine, but at runtime you
get a class case exception, saying that
returnTest is returning an object. Can someone
please explain to me why this is happening,
because my casting should be correct, but
obviously their is something strange I am missing.
Thanks,


-Matt<br /> SCJP2<br /> SCJD
Cedric Bompart
Greenhorn

Joined: May 22, 2002
Posts: 5
just do that
(DataInfo[]) x.toArray(new DataInfo[x.size()]);
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Cedric,
Thank's a lot that snippet worked great, but I am curious as to why it worked. Can you explain what is happening, because it seems slightly redundant to me, but at the same time it works your way and it doesn't my why, I am just curious as to the logic behind it.
Thanks again,
Cedric Bompart
Greenhorn

Joined: May 22, 2002
Posts: 5
Have a look in the JDK API, it's nicely explained
-> java.util.ArrayList , public Object[] toArray(Object[] a)
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
Thank's a lot!
-Matt
 
Consider Paul's rocket mass heater.
 
subject: Casting issue