Tag Archives: Arrays.asList() Error

[Solved] Arrays.asList() Error: java.lang.UnsupportedOperationException

1. Report an error

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.add(AbstractList.java:148)
	at java.util.AbstractList.add(AbstractList.java:108)
	at 

2. Reason

Before packaging items do when a utility class for converting between strings and arrays, abnormal is present in Arrays.asList()the.

public static List stringToList(String str, String separator) {
    List r = new ArrayList<>();
    if (BayouUtil.isNotEmpty(str)) {
        if (str.contains(separator)) {
            r = Arrays.asList(str.split(separator));	
        } else {
            r.add(str);
        }
    }
 
    return r;
}

Use asListis returned Arrayswithin the classArrayList

@SafeVarargs
@SuppressWarnings("varargs")
public static  List asList(T... a) {
    return new ArrayList<>(a);
}
 
private static class ArrayList extends AbstractList
    implements RandomAccess, java.io.Serializable
{
    private static final long serialVersionUID = -2764017481108945198L;
    private final E[] a;
 
    ArrayList(E[] array) {
        a = Objects.requireNonNull(array);
    }
......
}

And its successor, the AbstractListparent class add()remove()and clear() methods underlying the final will throw an exception.

public void add(int index, E element) {
    throw new UnsupportedOperationException();
}
 
public E remove(int index) {
    throw new UnsupportedOperationException();
}
 
private void rangeCheckForAdd(int index) {
    if (index < 0 || index > size())
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}

ListIn the interface add()remove()andclear()

image-20210810175025705

image-20210810175056375

Therefore, the use asListof add() remove() clear()other methods will ultimately throw UnsupportedOperationExceptionan exception.

And why ArrayList()Why does not complain? Because ArrayListthe rewrite add removeand clear methods

public void add(E e) {
    checkForComodification();
 
    try {
        int i = cursor;
        ArrayList.this.add(i, e);
        cursor = i + 1;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}
 
public void remove() {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();
 
    try {
        ArrayList.this.remove(lastRet);
        cursor = lastRet;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}
 
public Object clone() {
    try {
        ArrayList<?> v = (ArrayList<?>) super.clone();
        v.elementData = Arrays.copyOf(elementData, size);
        v.modCount = 0;
        return v;
    } catch (CloneNotSupportedException e) {
        // this shouldn't happen, since we are Cloneable
        throw new InternalError(e);
    }
}

3. Solution

r = new ArrayList<>(Arrays.asList(str.split(separator)));
// or
r.addAll(Arrays.asList(str.split(separator)));

Note: Arrays.asList() is a big pit, use it with caution