• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is this happening? (ClassCastException)

 
Ranch Hand
Posts: 47
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or the method toArray(T[] a)
 
Ronald Castillo
Ranch Hand
Posts: 47
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic