Skip to main content

Posts

Showing posts from August, 2017

Arrays.asList() throws java.lang.UnsupportedOperationException

Arrays.asList() throws java.lang.UnsupportedOperationException You might get the UnsupportedException when you tries to use the add() method in the List. If your code looks something similar to this : String[] data = new String[] {"hari","krushna"}; List<String> asList = Arrays.asList(data); asList.add("test"); Then you will see the following exception : java.lang.UnsupportedOperationException at java.util.AbstractList.add(Unknown Source) at java.util.AbstractList.add(Unknown Source) at ArrayConvert.trial(ArrayConvert.java:14) at ArrayConvert.main(ArrayConvert.java:8) Solution of the above problem is :  you need to create an ArrayList or LinkedList and add the objects to the newly created Array/LinkedList: String[] data = new String[] {"","",""}; List<String> asList = Arrays.asList(data); List<String> newList = new LinkedList<>(asList); newList.add("test"); Rea