Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Array) List implementations in Java

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?

like image 464
nagesh Avatar asked Dec 12 '18 02:12

nagesh


People also ask

What are implementations of List in Java?

There are two general-purpose List implementations — ArrayList and LinkedList .

Does ArrayList implement List?

ArrayList inherits AbstractList class and implements the List interface. ArrayList is initialized by the size.

What are the classes implemented using array and lists in Java?

The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector. ArrayList and LinkedList are widely used in Java programming.


3 Answers

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
like image 159
Aaron Avatar answered Nov 11 '22 14:11

Aaron


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
like image 43
Unmitigated Avatar answered Nov 11 '22 13:11

Unmitigated


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());
like image 45
Yash Avatar answered Nov 11 '22 14:11

Yash