• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Problem

 
Maanik kandan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to use Generic Collections to avoid these kind of unchecked exception
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic