aspose file tools*
The moose likes Beginning Java and the fly likes Casting Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting Problem" Watch "Casting Problem" New topic
Author

Casting Problem

Maanik kandan
Greenhorn

Joined: Aug 26, 2009
Posts: 4
Hi

is this possible to cast Array List to String



its give me a Exception like this..
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList

please give me a solution regarding my issue..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

Please take the time to choose the correct forum for your posts. This forum is for questions on JSP. For more information, please read this.

This post has been moved to a more appropriate forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61221
    
  66

No. You cannot cast an instance to anything that it is not. ArrayList isn't derived from String, so no. But it looks like your message is the opposite of what you stated.

Perhaps if you tell us what you are really trying to do with the list, someone can suggest a way to accomplish it.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

try to use Generic Collections to avoid these kind of unchecked exception
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

seetharaman venkatasamy wrote:try to use Generic Collections to avoid these kind of unchecked exception

You still won't be able to cast a String to ArrayList.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

To use a more concrete example...

you could cast a "Apple" object to a "Fruit". You could cast a "Chihuahua" to a "Dog".

You might be able to cast a "Fruit" to a "Pear", and you might be able to cast a "Dog" to a "Greyhound"

You can never cast a "Pumpkin" to an "Automobile", or an "Airplane" to a "GreatWhiteShark"

Casting doesn't magically change an object from one type to another. you can only cast up and down the hierarchy tree where the object lives - and when down-casting, you might run into problems if you take the wrong branch.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Problem