zoukankan      html  css  js  c++  java
  • Arrays.asList引起的惨案

    最近代码中需要对两个数组求交,想当然便用到了List中的retainAll函数,但要将将数组转换成list。代码如下:

    String[] abc = new String[] { "abc", "acd", "add" };
    String[] abd = new String[] {"acd", "cd", "de"};

    List<String> abcList = Arrays.asList(abc);
    List<String> abdList = Arrays.asList(abd);

    abcList.retainAll(abdList);

    没想到执行后却抛出了异常:

    java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:144)
    at java.util.AbstractList$Itr.remove(AbstractList.java:360)
    at java.util.AbstractCollection.retainAll(AbstractCollection.java:370)

    反编译后才发现原来 Arrays.asList返回的ArrayList并不是java.util.ArrayList,而是在Arrays类中重新定义的一下内部类ArrayList:

    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) {
    if (array==null)
    throw new NullPointerException();
    a = array;

    //.....其它的省略

    }

    这个ArrayList并没有重写remove方法。

    修改成如下,问题解决:

    String[] abc = new String[] { "abc", "acd", "add" };
    String[] abd = new String[] {"acd", "cd", "de"};

    List<String> abcList = new ArrayList<String>(Arrays.asList(abc));
    List<String> abdList = new ArrayList<String>(Arrays.asList(abd));

    abcList.retainAll(abdList);

  • 相关阅读:
    poj 3714 Raid(平面最近点对)
    hdu 4638 Group(离线+树状数组)
    UVa 10294(polya 翻转与旋转)
    hdu 4633 Who's Aunt Zhang(polya+逆元)
    Use of Function Arctan
    codeforces 299E Cube Problem
    UVa11806 Cheerleaders(容斥原理)
    UVa11538 A Chess Queen
    UVa11401
    周报(2017.3.19-3.16)
  • 原文地址:https://www.cnblogs.com/BensonHe/p/3604278.html
Copyright © 2011-2022 走看看