java-How to iterate and remove element from Arrays.asList result.
1. The purpose of this post
This post would demo how to solve this exception when we iterate and remove an element from an arraylist :
2. Environments
java 1.8+
3. How to reproduce this exception
If we run it, we got the above error.
4. How to solve this exception
Change your code like this:
rerun the code, then we get this:
5. Why
The asList() method of java.util.Arrays class is used to return a fixed-size list backed by the specified array. This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray().
So, if you want to change the result of the Arrays.asList, you must wrap it with a new ArrayList.