Arrays.asList(array) 和 new ArrayList<Integer>(Arrays.asList(array)) 的区别 [英] Difference between Arrays.asList(array) and new ArrayList&lt;Integer&gt;(Arrays.asList(array))

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

问题描述

有什么区别

  • Listlist1 = new ArrayList(Arrays.asList(ia));//复制

Listlist2 = Arrays.asList(ia);

,其中ia 是一个整数数组?

, where ia is an array of integers?

我开始知道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 does ArrayList being upcasted to list differ from creating a 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 的包装器,这使得原始数组可用作列表.没有复制任何东西,只创建一个包装对象.列表包装器上的操作会传播到原始数组.这意味着如果你打乱列表包装器,原始数组也会被打乱,如果你覆盖一个元素,它会在原始数组中被覆盖,等等.当然,一些 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 - 它是一种不同类型的对象.ArrayLists 有自己的内部数组,它们在其中存储它们的元素,并且能够调整内部数组的大小等.包装器没有自己的内部数组,它只将操作传播到给它的数组.

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 引用内存中的相同整数),但它创建了一个新的内部数组,用于保存引用.所以当你对它进行shuffle、添加、删除元素等操作时,原始数组不变.

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<Integer>(Arrays.asList(array)) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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