Scenario
Convert an array to a List using asList method of java.util.Arrays class.
Try to add an element to the list returned(by calling add method) or remove an element from the list(using remove method) and you will get an exception something like
Exception in thread “main” java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at TestList.main(TestList.java:15)
// create an array
Long[] array = {1l,2l};
//convert it to list
List list = Arrays.asList(array);
// add to list. This throws error
list.add(3L);
Reason
asList method of java.util.Arrays class returns an object of a java ArrayList which is nested inside the class java.util.Arrays.
ArrayList extends java.util.AbstractList and it does not implement add or remove method.
Thus, when these methods are called on this list object, it calls add or remove method of java.util.AbstractList class which in turn throws java.lang.UnsupportedOperationException.
From java docs of add method of java.util.AbstractList class
throws UnsupportedOperationException if the add operation is not supported by this list
Secondly, the list returned by asList method of java.util.Arrays class is a fixed-size list which means that elements cannot be added to or removed from the list. Java docs of asList method states
Returns a fixed-size list backed by the specified array
Arrays.asList method, it is Arrays$ArrayList<E>.Solution
Our ultimate goal is to create an ArrayList which has the contents of the array and which also supports add and remove operations over it.
There are a couple of ways to do it as follows:
Method 1 : Iterating over the array
This method is an inefficient method which involves creating a new ArrayList, iterating over the array and in every iteration adding the element of the array to the list.
Code is given below.
Long[] array = { 1l, 2l };
// create a new empty list
List newList = new ArrayList();
//iterate over array
for (int i = 0; i < array.length; i++) {
Long element = array[i];
//add array element to list
newList.add(element);
}
System.out.println(newList);
Output
Above code produces the following output
Method 2 : Creating an ArrayList directly with the contents of array
java.util.ArrayList has a constructor which takes another collection as argument.
Create a List using Arrays.asList method as you were using earlier and pass the resultant List to create a new ArrayList object.
This approach should be preferable over the first approach.
Long[] array={1l,2l};
//convert it to list
List list = Arrays.asList(array);
//create a new list with the contents of the above list
List newList = new ArrayList(list);
// add to list
newList.add(3L);
System.out.println(newList);
Output
Above code produces the following output
Let’s tweak in
- Any data structure which extends
java.util.Collectioninterface can be supplied while creating a newArrayList.
Thus, objects of typejava.util.Setsuch as HashSet, LinkedHashSet, objects of typejava.util.Listsuch asLinkedList,Vectorcan be used. - An
ArrayListuses an array behind the scenes. - The type of
ArrayListshould be the same as that of the List supplied to it in the constructor. - Though this post uses an
ArrayListof typejava.lang.Longbut it is applicable to list of all types such asjava.lang.String,java.lang.Integeretc.
It looks like you’ve misspelled the word “const” on your website. I thought you would like to know :). Silly mistakes can ruin your site’s credibility.
-Sarah
“const” is a javascript keyword used to declare constant values, you should ask javascript developers to rectify this 🙂