Before because of

I use Arrays in the actual development. AsList List is used, an array of strings can be converted to subsequent need to remove elements from the List according to the rule But remove the error Java. Lang. UnsupportedOperationException

String[] STRS = {" A"," B"," C"}; List<String> stringList = Arrays.asList(strs); StringList. Remove (" A ");Copy the code

Error: throw Java. Lang. UnsupportedOperationException

explore

  • Query the definition of arrays.aslist
/**

 * Returns a fixed-size list backed by the specified array.  (Changes to

 * the returned list "write through" to the array.)  This method acts

 * as bridge between array-based and collection-based APIs, in

 * combination with {@link Collection#toArray}.  The returned list is

 * serializable and implements {@link RandomAccess}.

 *

 * <p>This method also provides a convenient way to create a fixed-size

 * list initialized to contain several elements:

 * <pre>

 *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

 * </pre>

 *

 * @param <T> the class of the objects in the array

 * @param a the array by which the list will be backed

 * @return a list view of the specified array

 */

@SafeVarargs

@SuppressWarnings("varargs")

public static <T> List<T> asList(T... a) {

    return new ArrayList<>(a);

}
Copy the code

Returns a fixed-length List. This method is used to convert the array and Collection apis. The return value is simply a wraparound List of arrays.

Think In Java

AsList () directly, as a List, but the underlying representation in this case is the array, which cannot be resized. If you try to add( ) or delete( ) elements in such a list, That would attempt to change the size of an array, so you'll get an "Unsupported Operation" error at run time.Copy the code

New ArrayList() = new ArrayList(); Why can you add and remove an ArrayList when you normally define it? That’s because the new ArrayList is actually an inner class of their own implementation inside Arrays

 private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
Copy the code

That is to say,

  • Arrays. AsList returns an ArrayList that is actually an inner class of Arrays belonging to Java.util.Arrays$ArrayList
  • List List = new ArrayList belongs to java.util.ArraysList

Looking at the source code for ArrayList, I found that it does not implement the add and remove methods, so I went up one level to look at the source code for AbstractList

public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }
public E remove(int index) {
    throw new UnsupportedOperationException();
}
Copy the code

So throw the Java. Lang. UnsupportedOperationException

Arrays.aslist () can’t add or remove a List of Arrays

The solution

Use the new ArrayList < > (Arrays. AsList ())

Example: List List = new ArrayList<>(array.aslist (1,2,3));

conclusion

1. Arrays. AsList returns an array of fixed length can’t add and remove (conclusion), the add and remove returns the Java lang. UnsupportedOperationException

AbstractList < AbstractList > < AbstractList > < AbstractList > < AbstractList > < AbstractList Java. Lang. UnsupportedOperationException (reason), the real inherited AbstractList ArrayList, and realizes the add and remove methods

ArrayList<>(arrays.aslist ())

Example: List List = new ArrayList<>(array.aslist (1,2,3));