Arrays.asList(array)和new ArrayList< Integer>(Arrays.asList(array))之间的区别 [英] Difference between Arrays.asList(array) and new ArrayList<Integer>(Arrays.asList(array))

查看:552
本文介绍了Arrays.asList(array)和new ArrayList< Integer>(Arrays.asList(array))之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia));  //copy
2.List<Integer> list2 = Arrays.asList(ia);

其中 ia 是整数数组。

我发现 list2 中不允许进行某些操作。为什么会这样?
如何存储在内存中(引用/复制)?

I came to know that some operations are not allowed in list2. why is it so? how is it stored in memory (references / copy)?

当我随机播放列表时, list1 不影响原始数组,但 list2 确实如此。但仍然 list2 有点令人困惑。

When I shuffle the lists, list1 doesn't affect the original array but list2 does. But still list2 is somewhat confusing.

如何 ArrayList 如何升级到列表与创建新 ArrayList

How ArrayList being upcasted to list differs from creating new ArrayList

list1 differs from (1)
ArrayList<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia));


推荐答案


  1. 首先,让我们看看这是做什么的:

  1. First, let's see what this does:

Arrays.asList(ia)

它需要一个数组 ia 并创建一个实现 List< Integer>的包装器。 ,它使原始数组可用作列表。没有复制任何内容,只创建了一个包装器对象。列表包装器上的操作将传播到原始数组。这意味着如果你改组列表包装器,原始数组也会被洗牌,如果你覆盖一个元素,它会被原始数组覆盖,等等。当然,有些 List 包装器上不允许进行操作,比如在列表中添加或删除元素,只能读取或覆盖元素。

It takes an array ia and creates a wrapper that implements List<Integer>, which makes the original array available as a list. Nothing is copied and all, only a single wrapper object is created. Operations on the list wrapper are propagated to the original array. This means that if you shuffle the list wrapper, the original array is shuffled as well, if you overwrite an element, it gets overwritten in the original array, etc. Of course, some List operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.

请注意列表包装器没有扩展 ArrayList - 它是一种不同的对象。 ArrayList 有自己的内部数组,它们存储它们的元素,并且能够调整内部数组的大小等。包装器没有自己的内部数组,它只会将操作传播给给定的数组。

Note that the list wrapper doesn't extend ArrayList - it's a different kind of object. ArrayLists have their own, internal array, in which they store their elements, and are able to resize the internal arrays etc. The wrapper doesn't have its own internal array, it only propagates operations to the array given to it.

另一方面,如果你随后创建一个新数组

On the other hand, if you subsequently create a new array as

new ArrayList<Integer>(Arrays.asList(ia))

然后你创建新的 ArrayList ,这是原始的一个完整的独立副本。虽然这里使用 Arrays.asList 创建包装器,但它仅在构造新的 ArrayList 时使用然后被垃圾收集。这个新的 ArrayList 的结构完全独立于原始数组。它包含相同的元素(原始数组和新的 ArrayList 引用内存中的相同整数),但它创建了一个新的内部数组,用于保存引用。因此,当您对其进行随机播放,添加,删除元素等时,原始数组将保持不变。

then you create new ArrayList, which is a full, independent copy of the original one. Although here you create the wrapper using Arrays.asList as well, it is used only during the construction of the new ArrayList and is garbage-collected afterwards. The structure of this new ArrayList is completely independent of the original array. It contains the same elements (both the original array and this new ArrayList reference the same integers in memory), but it creates a new, internal array, that holds the references. So when you shuffle it, add, remove elements etc., the original array is unchanged.

这篇关于Arrays.asList(array)和new ArrayList&lt; Integer&gt;(Arrays.asList(array))之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆