Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

I'm new to Java and am trying to understand why the first code snippet doesn't cause this exception but the second one does. Since a string array is passed into Arrays.asList in both cases, shouldn't both snippets produce an exception or not produce an exception?

Exception in thread "main" java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList 

First snippet (causes no exception):

ArrayList<ArrayList<String>> stuff = new ArrayList<ArrayList<String>>(); String line = "a,b,cdef,g"; String delim = ","; String[] pieces = line.split(delim); stuff.add((ArrayList<String>) Arrays.asList(pieces)); 

Second snippet (causes above exception):

ArrayList<ArrayList<String>> stuff = new ArrayList<ArrayList<String>>(); String[] titles = {"ticker", "grade", "score"}; stuff.add((ArrayList<String>) Arrays.asList(titles)); 

If relevant, I'm using JavaSE 1.6 in Eclipse Helios.

like image 957
DannyTree Avatar asked Jul 10 '11 12:07

DannyTree


People also ask

What causes a ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

Can we cast list to ArrayList in Java?

Convert list To ArrayList In Java. ArrayList implements the List interface. If you want to convert a List to its implementation like ArrayList, then you can do so using the addAll method of the List interface.

What is Java Util arrays ArrayList?

The java. util. Arrays$ArrayList is a nested class inside the Arrays class. It is a fixed size or immutable list backed by an array.


2 Answers

For me (using Java 1.6.0_26), the first snippet gives the same exception as the second one. The reason is that the Arrays.asList(..) method does only return a List, not necessarily an ArrayList. Because you don't really know what kind (or implementation of) of List that method returns, your cast to ArrayList<String> is not safe. The result is that it may or may not work as expected. From a coding style perspective, a good fix for this would be to change your stuff declaration to:

List<List<String>> stuff = new ArrayList<List<String>>(); 

which will allow to add whatever comes out of the Arrays.asList(..) method.

like image 70
Waldheinz Avatar answered Oct 11 '22 04:10

Waldheinz


If you do this, you won't get any CCE:

ArrayList<ArrayList<String>> stuff = new ArrayList<ArrayList<String>>(); String[] titles = {"ticker", "grade", "score"}; stuff.add(new ArrayList<String>(Arrays.asList(titles))); 

As the error clearly states, the class java.util.ArrayList isn't the same as nested static class java.util.Arrays.ArrayList. Hence the exception. We overcome this by wrapping the returned list using a java.util.ArrayList.

like image 36
adarshr Avatar answered Oct 11 '22 04:10

adarshr