• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ClassCastException ArrayList->List

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
a simple question for you.

<code>
import java.util.*;
public class TestArrayList {
public static void main(String[] args) {

String[] as = {"one","two","three","four"};
ArrayList <String> al;
al = (ArrayList)Arrays.asList(as);
al.set(2,"another number");

Iterator<String> i = al.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}

for(String temp : as){
System.out.println(temp);
}

}
}
</code>

This class raises a ClassCastException at line
al = (ArrayList)Arrays.asList(as);

I thought that I could cast because "ArrayList instanceof List" is true(List is the type returned by Arrays.asList(array))

Please, where am I wrong?
Thanks,
Gianni
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Arrays.asList method simply returns a List, not necessarily an ArrayList.
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This clarify, thanks.
But then another question:
List is an interface, so it doesn't provide implementations of get and set method, am I right?
So how can we invoke get and set methods directly from a List?
example:


Thank you !
Gianni
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ipez,
Because al accepts only strings while casting

___________________________________________________
al = (ArrayList)Arrays.asList(as);
ArrayList <String> al;
___________________________________________________

do like this:

al = (ArrayList<String> Arrays.asList(as);

I am not sure but i hope it works.

Thanks,
Kris.
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It passes back a list inteface, hence you can call methods present in List (polymorphism).

Add the following line
System.out.println(al.getClass());

what you get is
class java.util.Arrays$ArrayList

Obviously it is not an java.util.ArrayList but an inner private class ArrayList in java.util.Arrays.
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you!
another little java miracle.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic