Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at
之前做项目的时候封装了一个工具类,用来数组和字符串之间的转换,发现异常是出现在 Arrays.asList()
中。
工具类详情见:Java 字符串和数组转换工具类
/**
* 将字符串按指定字符串分割存入到List中
*
* @param str [字符串]
* @param separator [分隔符]
* @return java.util.List<java.lang.String>
*/
public static List<String> stringToList(String str, String separator) {
List<String> r = new ArrayList<>();
if (BayouUtil.isNotEmpty(str)) {
if (str.contains(separator)) {
// 报错主要是由这里Arrays.asList()引起的
r = Arrays.asList(str.split(separator));
} else {
r.add(str);
}
}
return r;
}
使用 asList
返回的是 Arrays
的内部类 ArrayList
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
......
}
而其继承的 AbstractList
父类中的 add()
, remove()
和 clear()
方法中的底层最后都会抛出异常 。
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));
}
List
接口中的 add()
, remove()
和 clear()
因此在使用 asList
中的 add()
remove()
clear()
等方法最后都会抛出 UnsupportedOperationException
异常。
而为什么 ArrayList()
为什么不会报错?因为 ArrayList
中重写了 add
remove
和 clear
方法
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);
}
}
r = new ArrayList<>(Arrays.asList(str.split(separator)));
// 或者
r.addAll(Arrays.asList(str.split(separator)));
注意:Arrays.asList()是个大坑,谨慎使用
原创博主:Little Tomcat
博主原文链接:https://mp.weixin.qq.com/s/p4wk0nlv7NCeT0694Se1Pw
关于Arrays.asList() 中的java.lang.UnsupportedOperationException异常
原文:https://www.cnblogs.com/d-sixteen/p/15126317.html