• Post Reply Bookmark Topic Watch Topic
  • New Topic

linkedlist  RSS feed

 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question isnot on linkedlist in specific. But the example is using it.
Why the following code gives class cast exception, despite of that
Class linkedlist extends abstractlist implements list

Code is
LinkedList<String> link = (LinkedList) Arrays. asList(any array of string);
All of them are in the same hierarchy!!
 
Paweł Baczyński
Bartender
Posts: 2086
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:All of them are in the same hierarchy!!

No, they are not.
Arrays.asList() returns java.util.Arrays.ArrayList (at least on my version of JDK) which is-not-a LinkedList. So, you can't make this cast.
If you really need a LinkedList you could do something like this:
new LinkedList<String>(Arrays.asList(anyStringArray));
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot . You are right. Because they must be of different types t get such errors but what confused me that the api docs declare the return type is list but it must be an array list like you said.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it need not be an ArrayList - it must be a List, because that's what the API specifies. Pawel just mentioned what the JRE he's using does; any other JRE may choose to return a different List implementation (although, in practice, they probably all return an ArrayList, but you can't depend on that).
 
Mohamad Samy
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok according to you. My netbeans returns a list so why the downcasting give this type f exception
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays.asList(...) returns a List, as the API documentation says. But it does not specify what kind of List. Nobody promised you that it is a LinkedList, so you can try to cast it to LinkedList, but if it isn't (and it turns out that it is indeed not a LinkedList), you get a ClassCastException.

It's the old "a cow is an animal, but not every animal is a cow" thing: a LinkedList is a List, but not every List is a LinkedList.

Remember that casting does not automatically convert objects. So, a cast is not automatically going to convert any kind of List into a LinkedList. Casting only means that you tell the compiler: "don't check the actual type, just treat this object as this type that I tell you". At runtime, the type will still be checked, and if it's wrong, you get a ClassCastException.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamad Samy wrote:My netbeans returns a list

No it doesn't. No method can ever return a List becuase it is an interface. The method returns an instance of a class that implements the List interface. In Pawel's case it's a java.util.Arrays.ArrayList*. With another JRE it could be a different class - the only thing you can say for certain about the class is that it implements the List interface.


* Note that this is not the same class as the standard Java ArrayList - that is java.util.ArrayList. So you can't even cast the return value to an java.util.ArrayList without risking a ClassCastException at runtime..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!