• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is this happening? (ClassCastException)  RSS feed

 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at the javadoc for toArray().
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or the method toArray(T[] a)
 
Ronald Castillo
Ranch Hand
Posts: 47
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!