Table of Contents
In this post, we will see how to fix java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList.
ClassCastException
is runtime exception which indicate that code has tried to cast an object to a subclass which is not an instance.
Reason for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Although static method Arrays.asList()
returns List, it returns java.util.Arrays$ArrayList
which is different from ArrayList, so when we try to cast Arrays.asList()
to ArrayList
, it throws exception.
Let’s understand with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Arrays; public class ArrayToList { public static void main(String[] args) { String[] countries = {"India","China","Bhutan"}; ArrayList<String> listOfCountries = (ArrayList)Arrays.asList(countries); System.out.println(listOfCountries); } } |
Output:
1 2 3 4 |
Exception in thread "main" java.lang.ClassCastException: class java.util.Arrays$ArrayList cannot be cast to class java.util.ArrayList (java.util.Arrays$ArrayList and java.util.ArrayList are in module java.base of loader 'bootstrap') at org.arpit.java2blog.ArrayToList.main(ArrayToList.java:11) |
Here is source code of Arrays.asList()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@SafeVarargs @SuppressWarnings("varargs") public static <T> List<T> asList(T... a) { return new ArrayList<>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { ... } |
As you can see that there is static inner class present in java.util.Arrays which is different from java.util.ArrayList.
Fixes for java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Use ArrayList’s constructor
If you must have instance of java.util.ArrayList, then you can create ArrayList instance using ArrayList‘s constructor.
1 2 3 |
ArrayList<String> listOfCountries = new ArrayList(Arrays.asList(countries)); |
Here is complete program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Arrays; public class ArrayToList { public static void main(String[] args) { String[] countries = {"India","China","Bhutan"}; ArrayList<String> listOfCountries = new ArrayList(Arrays.asList(countries)); System.out.println(listOfCountries); } } |
Output:
Here is source code of ArrayList’s constructor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's * iterator. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } |
Assign Arrays.asList() to List reference rather than ArrayList
We can declare List rather than ArrayList to avoid the exception. Since java.util.Arrays$ArrayList implements List interface, we can assign it to List reference.
1 2 3 |
List<String> listOfCountries = Arrays.asList(countries); |
Here is complete program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package org.arpit.java2blog; import java.util.Arrays; import java.util.List; public class ArrayToList { public static void main(String[] args) { String[] countries = {"India","China","Bhutan"}; List<String> listOfCountries = Arrays.asList(countries); System.out.println(listOfCountries); } } |
Output:
That’s all about how to fix [Fixed] java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList.