Arrays.asList()返回的List调用add()和addAll()

Arrays.asList()返回的List调用add()和addAll()
错误信息
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at java.util.AbstractCollection.addAll(AbstractCollection.java:344)
解决

查看源码后发现,Arrays.asList()返回的ArrayList是Arrays类的内部类(ArrayList),asList()里是把数据放到private final E[] a;里的,final修饰的数组,长度是不可变的,所以不可新增或删除。

jdk源码

Arrays.java

@SafeVarargs
    @SuppressWarnings("varargs")
    public static <T> List<T> asList(T... a) {
        return new ArrayList<>(a);
    }

    /**
     * @serial include
     */
    private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        //使用final修饰的数组
        private final E[] a;

        ArrayList(E[] array) {
            a = Objects.requireNonNull(array);
        }

        @Override
        public int size() {
            return a.length;
        }

        @Override
        public Object[] toArray() {
            return a.clone();
        }

        @Override
        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }

        @Override
        public E get(int index) {
            return a[index];
        }

        @Override
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }

        @Override
        public int indexOf(Object o) {
            E[] a = this.a;
            if (o == null) {
                for (int i = 0; i < a.length; i++)
                    if (a[i] == null)
                        return i;
            } else {
                for (int i = 0; i < a.length; i++)
                    if (o.equals(a[i]))
                        return i;
            }
            return -1;
        }

        @Override
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }

        @Override
        public Spliterator<E> spliterator() {
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }

        @Override
        public void forEach(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            for (E e : a) {
                action.accept(e);
            }
        }

        @Override
        public void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
            E[] a = this.a;
            for (int i = 0; i < a.length; i++) {
                a[i] = operator.apply(a[i]);
            }
        }

        @Override
        public void sort(Comparator<? super E> c) {
            Arrays.sort(a, c);
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这段代码是一个寻找四个数之和等于目标值的算法。它使用了递归的方法来遍历数组,找到符合条件的四个数,并将其添加到一个ArrayList中。最后输出这个ArrayList。 代码中存在一个问题:在递归调用panduan方法时,没有对返回值进行处理,导致无法正确地将结果添加到myAry中。需要将递归调用的结果添加到myAry中,可以使用addAll方法。 修改后的代码如下: ```java public static void main(String[] args) { int[] nums = {-3, -1, 0, 2, 4, 5}; int target = 0; ArrayList<List<Integer>> myAry = new ArrayList<>(); long adds = 0; Arrays.sort(nums); if (nums.length < 4) { System.out.println("g"); } int zuo = 0; int you = nums.length - 1; int x = zuo + 1; int y = you - 1; myAry.addAll(panduan(adds, zuo, y, x, you, nums, target)); System.out.println(myAry); } public static List<List<Integer>> panduan(long adds, int zuo, int y, int x, int you, int[] nums, int target) { x = zuo + 1; y = you - 1; List<List<Integer>> myAry = new ArrayList<>(); if(x == y){ return myAry; } while (true) { while (x <= y) { adds = nums[zuo] + nums[x] + nums[y] + nums[you]; if (adds == target) { if (myAry.size() == 0) { myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you])); } else { if (!myAry.contains(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you]))) { myAry.add(Arrays.asList(nums[zuo], nums[x], nums[y], nums[you])); } } x += 1; } else if (adds > target) { y -= 1; } else { x += 1; } } x = zuo + 1; y = you - 1; if (nums[zuo] + nums[x] + nums[y] + nums[you] < target) { myAry.addAll(panduan(adds, zuo+1, y, x, you, nums, target)); } else if (nums[zuo] + nums[x] + nums[y] + nums[you] > target) { myAry.addAll(panduan(adds, zuo, y, x, you-1, nums, target)); } else { myAry.addAll(panduan(adds, zuo, y, x, you-1, nums, target)); myAry.addAll(panduan(adds, zuo+1, y, x, you, nums, target)); } if (zuo + 1 == you - 1) { break; } } return myAry; } ``` 这样修改后,代码能够正确地输出符合条件的四个数的组合。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zarathusa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值