6

Issue with array list implementation:

My code

List<Integer> arrayList=new ArrayList<Integer>(3);
arrayList=Arrays.asList(10,20);
System.out.println(arrayList.size());
//arrayList.add(30);
System.out.println(arrayList.size());

I am getting unsupportedException at line 4

What is the issue?

1
  • 2
    Good to see new contributors. The answer by Aaron is correct. anyway this has nothing to do with spring boot so edited the question a little. Happy learning and coding! Do a tutorial on Java basics (Java core as we call it). Or best read a book cover to cover. Commented Dec 12, 2018 at 4:00

4 Answers 4

4

Arrays.asList returns a fixed-size of array, which is a direct subclass of AbstractList and apparently does not support add and remove functions..

You could try ArrayList instead of Arrays.asList if you intend to perform add or remove functions later; both of them are types of List but have different implementations.

List<Integer> arrayList = new ArrayList<Integer>(3);
Collections.addAll(arrayList, 10, 20);
System.out.println(arrayList.size()); // size = 2
arrayList.add(30); // OK
System.out.println(arrayList.size()); // size = 3
Sign up to request clarification or add additional context in comments.

1 Comment

Since the OP did already use an ArrayList, it’s worth emphasizing that the assignment arrayList=Arrays.asList(10,20) overwrites the reference to that ArrayList with a reference to another object, hence stops the use of the ArrayList. So replacing arrayList=Arrays.asList(10,20) with Collections.addAll(arrayList, 10, 20) would solve the issue.
1

Array.asList returns a collection that is not fully modifiable. To add or remove elements, you can use the ArrayList constructor which accepts a Collection to wrap the collection returned by Arrays.asList.

List<Integer> arrayList = new ArrayList(Arrays.asList(10,20));
System.out.println(arrayList.size()); //2
arrayList.add(30);
System.out.println(arrayList.size()); //3

Comments

1

When you are converting from an Array to a Collection Obejct. i.e., array-based to collection-based API then it is going to provide you fixed-size collection object, because Array's behaviour is of Fixed size.

java.util.Arrays.asList( T... a )

Souce samples for conformation.

public class Arrays {
    public static <T> List<T> asList(T... a) {
        return new java.util.Arrays.ArrayList.ArrayList<>(a); // Arrays Inner Class ArrayList
    }
    //...
    private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
        //...
    }
}
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }
    public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }
    public E remove(int index) {
        throw new UnsupportedOperationException();
    }
}

Form the above Source you may observe that java.util.Arrays.ArrayList class doesn't @Override add(index, element), set(index, element), remove(index). So, From inheritance it executes super class add() function which throws UnsupportedOperationException.

As AbstractList<E> is an abstract class it provides the implementation to iterator() and listIterator() so that we can iterate over the list object.

List<String> list_of_Arrays = Arrays.asList(new String[] { "a", "b" ,"c"});

try {
    list_of_Arrays.add("Yashwanth.M");
} catch(java.lang.UnsupportedOperationException e) {
    System.out.println("List Interface executes AbstractList add() fucntion which throws UnsupportedOperationException.");
}
System.out.println("Arrays → List : " + list_of_Arrays);

Iterator<String> iterator = list_of_Arrays.iterator();
while (iterator.hasNext()) System.out.println("Iteration : " + iterator.next() );

ListIterator<String> listIterator = list_of_Arrays.listIterator();
while (listIterator.hasNext())    System.out.println("Forward  iteration : " + listIterator.next() );
while(listIterator.hasPrevious()) System.out.println("Backward iteration : " + listIterator.previous());

Comments

0

Pass the array in the ArrayList's constructor. For Example:

List<Integer> arrayList=new ArrayList<Integer>(Arrays.asList(10,20));
System.out.println(arrayList.size());
arrayList.add(30);
System.out.println(arrayList.size()); 

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.