| Author |
Why is this happening? (ClassCastException)
|
Ronald Castillo
Ranch Hand
Joined: Apr 16, 2011
Posts: 47
|
|
I was doing some of the workshop content on blackbeltfactory.com site, the "List and Arrays" one.
When given the specs I searched through the API and managed to find many of the requests are already implemented.
However, I'm stuck at this:
I'm supposed to do a method that takes a String[] array and returns an ArrayList<String> with the same objects and with the same order
This is the way I implemented my ArrayUtils class:
However, when doing:
on my UtilsTester class, I get a ClassCastException:
Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
at net.ronaldcastillo.utils.ArrayUtils.toArrayList(ArrayUtils.java:20)
at net.ronaldcastillo.UtilsTester.main(UtilsTester.java:15)
Why is that happening?
Arrays.asList() is supposed to return a List, so that means I can correctly cast it to an ArrayList (I guess), but I'm getting this exception.
Could you explain why?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
Your assumption in wrong. Arrays.asList doesn't return an ArrayList but an internal class that implements the List interface. Therefore you can't cast it to an ArrayList.
If you need an ArrayList you could do something like:
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56212
|
|
|
And why do you have the urge to cast it as an ArrayList in the first place? Why isn't the List interface acceptable? Does the requirement really say it has to be a specific List implementation like ArrayList? (Bad form if so -- coding to interfaces is much more versatile.)
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Ronald Castillo
Ranch Hand
Joined: Apr 16, 2011
Posts: 47
|
|
Bear Bibeault wrote:And why do you have the urge to cast it as an ArrayList in the first place. Why isn't the List interface acceptable? Does the requirement really say it has to be an ArrayList? (Bad form if so -- coding to interfaces is much more versatile.)
Since the requirement said the method should return an ArrayList<String> that's why I tried it that way. If it was up to me, I would have used a List as you say. "Program to an interface, not an implementation".
I could run the code successfully, now, how can I do the opposite:
Is also throwing me a ClassCastException:
[Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56212
|
|
|
Look carefully at the javadoc for toArray().
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
Or the method toArray(T[] a)
|
 |
Ronald Castillo
Ranch Hand
Joined: Apr 16, 2011
Posts: 47
|
|
Bear Bibeault wrote:Look carefully at the javadoc for toArray().
Thanks for the hint, here's the solution:
Suppose l is a List known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] x = (String[]) v.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
|
 |
 |
|
|
subject: Why is this happening? (ClassCastException)
|
|
|