This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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,
just do that (DataInfo) x.toArray(new DataInfo[x.size()]);
Joined: Feb 24, 2002
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,
Joined: May 22, 2002
Have a look in the JDK API, it's nicely explained -> java.util.ArrayList , public Object toArray(Object a)