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?
There are two general-purpose List implementations — ArrayList and LinkedList .
ArrayList inherits AbstractList class and implements the List interface. ArrayList is initialized by the size.
The implementation classes of the List interface are ArrayList, LinkedList, Stack, and Vector. ArrayList and LinkedList are widely used in Java programming.
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
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
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With