This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting issue
 
Similar Threads
Un reachable statement (Sorry for posting the same topic second time...)
Un reachable statement
Why is it so
an exception
finally