Particular part of the code was similar to this,
String[] inputArray = new String[]{"1", "2", "3"};
List strList = Arrays.asList(inputArray);
strList.add("4");
I get a UnsupportedOperationException when I tried to add a new element "4".
This exception comes because Arrays.asList() methods returns a Arrays.ArrayList class, which differs from the java.util.ArrayList implementation. Arrays.ArrayList is immutable by nature, so that we cannot add/delete any elements from the ArrayList.
String[] inputArray = new String[]{"1", "2", "3"};
List strList = new ArrayList();
strList.addAll(Arrays.asList(inputArray));
strList.add("4");
So we can simply follow this way to avoid the UnsupportedOperationException issue. In future, make sure you will be getting a fixed size ArrayList, when you initiate the ArrayList in the following way.
List strList = Arrays.asList(inputArray);
No comments:
Post a Comment