ArrayList vs Arrays.ArrayList

ArrayList vs Arrays.ArrayList

在一次部署中,发现如下代码竟然抛出来了UnsupportedException

List<String> srcList = Arrays.asList(new String[]{"a", "b", "c"})
List<String> toRemoveList = srcArrays.asList(new String[]{"a", "b"})
srcList.removeAll(toRemoveList)

点进去Arrays.asList方法,发现返回的是new ArrayList(a)啊。但是发现返回的是Arrays的内部类ArrayList,并非我们常用java.util.ArrayList.

内部类ArrayList继承了AbstractList类,但是没有实现remove接口,所以在removeAll的时候就会抛异常。

附上Arrays.asList的doc

/**
 * 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&lt;String&gt; 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
 */
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值