A guide to avoiding pitfalls in Java data structures

Table of contents

immutable array

Arrays.asList() does not support add and remove operations


 

immutable array

Arrays.asList() does not support add and remove operations

UnsupportedOperationException: add|remove

The main reason for this error is that the asList method of the java.util.Arrays class returns an ArrayList object nested in the class java.util.Arrays. ArrayList extends java.util.AbstractList, but does not implement add or remove methods. So when this method is called on a list object, it calls the Add or Remove method of the AbstractList class, thus throwing this exception. Also, the list returned by the asList method is a fixed-size list and therefore cannot be modified.
The example below when trying to add will result in

Reference: How to Solve Java List UnsupportedOperationException? - GeeksforGeeks

Follow-up will continue to add.....

Guess you like

Origin blog.csdn.net/boonya/article/details/130474737