Tag Archives: Array

ArrayList reported an error: Unsupported operationexception

Background:

When using the mybatis plus plug-in to interact the springboot project with the MySQL database, the list & lt; object> The type is directly put into the database through the user-defined converter, and the display is successful. However, in the add operation of the list, the unsupported operationexception is returned,

Specific reasons:

The ArrayList in the source code of arrays.aslist in the figure above is not our commonly used ArrayList. Our commonly used ArrayList is java.util.arraylist, while the new in the figure above is java.util.arrays.arraylist, which is an internal class under the arrays class. Its class declaration is as follows:

 

 

It can be seen that both it and java.util.arraylist inherit from the abstractlist abstract class, but it does not implement the add method and remove method. When we call the add method, it actually calls the add method of the parent abstractlist.

 

 

Add calls the add method with two parameters.

 

 

Then you will see throw new unsupported operationexception();

Solution:

To solve the above problem, just put the list into Java. Util. ArrayList, list & lt; Integer> Lists = new ArrayList (list), and then you can use lists to do various operations. Programmers should be careful and look at the source code