List在调用add、remove方法后报java.lang.UnsupportedOperationException

27 篇文章 1 订阅
20 篇文章 0 订阅

今天工作中碰到List对象调用add、remove方法竟然报错,用了好几年了尽然一直没发现,不知道之前写了多少bug。

在使用Arrays.asList()后调用add,remove这些method时出现java.lang.UnsupportedOperationException异常。这是由于Arrays.asList() 返回java.util.Arrays A r r a y L i s t , 而 不 是 A r r a y L i s t 。 A r r a y s ArrayList,而不是ArrayList。Arrays ArrayListArrayListArraysArrayList和ArrayList都是继承AbstractList,remove,add等method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。ArrayList override这些method来对list进行操作,但是Arrays$ArrayList没有override remove(),add()等,所以throw UnsupportedOperationException。

Arrays源码分析:

    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

Arrays.asList()方法返回的是Arrays.ArrayList对象,而不是java.util.ArrayList对象。Arrays.ArrayList和java.util.ArrayList类都继承AbstractList抽象类,Arrays.ArrayList类没有重写add、remove等方法,java.util.ArrayList类重写了add、remove等方法。

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

使用Arrays.asList()方法返回的对象去调用add、remove方法时,实际调用的是AbstractList抽象类的,该类的实现如下:

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

public E remove(int index) {
    throw new UnsupportedOperationException();
}

所以,使用Arrays.asList()返回对象调用add、remove方法时直接报错。

代码如下:

public void updateUserTodayHasNoReadWord(String openId, String word, int flag) {
	String dateStr = TimeUtils.formatDateToStr(new Date(),"yyyyMMdd");
	String key = Constants.POCKET_YUEDU_USER_NO_READ_WORD_KEY + openId + "_" + dateStr;
	List<String> noReadWordList = new ArrayList<>();
	String noReadWord = redisTemplate.get(key);// 今日未读单词
	if (org.apache.commons.lang.StringUtils.isNotBlank(noReadWord)) {
		noReadWordList = Arrays.asList(noReadWord.split(","));
		noReadWordList.remove(word);
		noReadWord = String.join(",", noReadWordList);
		redisTemplate.setex(key, Constants.REDIS_TIME_OUT_ONE_DAY, noReadWord);
	}
}

运行代码后,在调用remove()方法处报错,java.lang.UnsupportedOperationException异常。

解决方法:

public void updateUserTodayHasNoReadWord(String openId, String word, int flag) {
	String dateStr = TimeUtils.formatDateToStr(new Date(),"yyyyMMdd");
	String key = Constants.POCKET_YUEDU_USER_NO_READ_WORD_KEY + openId + "_" + dateStr;
	List<String> noReadWordList = new ArrayList<>();
	String noReadWord = redisTemplate.get(key);// 今日未读单词
	if (org.apache.commons.lang.StringUtils.isNotBlank(noReadWord)) {
		noReadWordList = new ArrayList<>(Arrays.asList(noReadWord.split(",")));
		noReadWordList.remove(word);
		noReadWord = String.join(",", noReadWordList);
		redisTemplate.setex(key, Constants.REDIS_TIME_OUT_ONE_DAY, noReadWord);
	}
}

参考
When you call Arrays.asList it does not return a java.util.ArrayList. It returns a java.util.Arrays$ArrayList which is an immutable list. You cannot add to it and you cannot remove from it.

If you want a mutable list built from your array you will have to loop over the array yourself and add each element into the list in turn.

Even then your code won’t work because you’ll get an IndexOutOfBoundsException as you remove the elements from the list in the for loop. There are two options: use an Iterator which allows you to remove from the list as you iterate over it (my recommendation as it makes the code easier to maintain) or loop backwards over the loop removing from the last one downwards (harder to read).

You are using AbstractList. ArrayList and Arrays A r r a y L i s t a r e b o t h t y p e s o f A b s t r a c t L i s t . T h a t ′ s w h y y o u g e t U n s u p p o r t e d O p e r a t i o n E x c e p t i o n : A r r a y s ArrayList are both types of AbstractList. That&#x27;s why you get UnsupportedOperationException: Arrays ArrayListarebothtypesofAbstractList.ThatswhyyougetUnsupportedOperationException:ArraysArrayList does not override remove(int) so the method is called on the superclass, AbstractList, which is what is throwing the exception because this method is not implemented on that class (the reason being to allow you to build immutable subclasses).

另外:List转换成字符串并加入分隔符,可以StringUtils的join方法
public String listToString(List list, char separator) {
   org.apache.commons.lang.StringUtils.join(list.toArray(),separator);
}

转载自:http://blog.csdn.net/fygkchina/article/details/32329293
————————————————
版权声明:本文为CSDN博主「刘彦民」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014740338/article/details/78534200

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值