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?

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
nagesh
  • 61
  • 2
  • 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. – tgkprog Dec 12 '18 at 04:00

4 Answers4

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
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Aaron
  • 3,764
  • 2
  • 8
  • 25
  • 2
    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. – Holger Dec 12 '18 at 16:16
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
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
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());
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Yash
  • 9,250
  • 2
  • 69
  • 74
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()); 
Madhubala
  • 46
  • 1
  • 7