Forums Register Login

linkedlist

+Pie Number of slices to send: Send
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!!
+Pie Number of slices to send: Send
 

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));
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
Ok according to you. My netbeans returns a list so why the downcasting give this type f exception
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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..
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 865 times.
Similar Threads
linked lists
Convert LinkList to a Array
Genrics Array creation
code for a recently used menu
Collection.toArray() method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:51:37.