2

Arrays class contain a function to convert arrays into list.When i convert the array of Integer into ArrayList it will throw an exception.

Integer[] array = new Integer[]{2, 4, 3 , 9};
ArrayList<Integer> test = (ArrayList<Integer>) Arrays.asList(array);

The Arrays.asList(array) return a List of type Integer, when i convert the list of Integer to ArrayList, it will throw an exception

 java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

ArrayList implements the List interface, so why this throw an exception ?

When i try catch the object direct with List reference variable, this work fine.

4 Answers 4

4

Arrays.asList returns a customly defined interpretation of List, it isn't java.util.ArrayList, but an AbstractList that is fixed-sized.

You will need to wrap all of the content in a new list:

List<Integer> newList = new ArrayList<>(Arrays.asList(array));
Sign up to request clarification or add additional context in comments.

7 Comments

"...but an AbstractList that cannot be modified." There's nothing in the javadoc saying it's an AbstractList. All you know is that it's a List.
@T.J.Crowder I'm going based off the class' source code: GrepCode
Java8: Source says it return an instance of an inner private ArrayList class which extends AbstractList.. private static class ArrayList<E> extends AbstractList<E> implements
@MarcoAcierno & Rogue: It doesn't matter what the source says, it matters what the contract says. The contract is the declaration and the doc, not the source of a particular implementation.
Despite what the documentation might state it isn't a modifiable collection that is returned. If you attempt to do work on it, you will have a runtime exception thrown.
|
2

Arrays#asList doesn't return an ArrayList<T>, it returns a List<T>. You don't know anything about the implementation of the List, just that it adheres to the List contract, with the documented limitations (the List is fixed-size, so presumably doesn't implement the optional operations like List#add that would change its size).

Comments

2

static <T> Array.asList method returns a List<T>, not an ArrayList<T>. You cannot assume that the implementation is going to return an ArrayList<T> implementation of List<T> - that's why you are getting an exception.

The error indicates that the implementation returns an instance of an inner class defined in the Arrays class; the inner class is called ArrayList as well, but it has nothing to do with the ArrayList<T> defined in java.utils. A simple cast between the two will not work - if you must have a java.util.ArrayList<T>, you need to create it yourself from the List<T> returned by the asList method.

Comments

0

You can initialize a new ArrayList with a List as parameter:

Integer[] ints = new Integer[]{2,4,3,9};
List<Integer> temp = Arrays.asList( ints );
ArrayList<Integer> testList = new ArrayList<Integer>(temp);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.