Java中Arrays.asList(array)和new ArrayList之间的区别< Integer>(Arrays.asList(ia)) [英] Difference between Arrays.asList(array) vs new ArrayList<Integer>(Arrays.asList(ia)) in java
问题描述
之间的区别是什么?
1.List< Integer> list1 = new ArrayList< Integer>(Arrays.asList(ia)); // copy
2.List< Integer> list2 = Arrays.asList(ia);
其中 ia
是整数数组。 / p>
我知道在 list2
中不允许有些操作。为什么会这样呢?
它是如何存储在内存中的(引用/复制)?
当我随机播放列表时, list1
不会影响原始数组,但 list2
会影响。但仍然 list2
有点混乱。
如何被上传到列表不同于创建新
ArrayList
list1不同于(1)
ArrayList< Integer> list1 = new ArrayList< Integer>(Arrays.asList(ia));
-
,让我们看看这是做什么:
Arrays.asList(ia)
pre>
它需要一个数组
ia
,并创建一个包装器实现List< Integer>
,这使得原始数组可用作列表。没有复制和全部,只创建一个包装器对象。列表包装器上的操作将传播到原始数组。这意味着,如果你打乱列表包装器,原始数组也被洗牌,如果你覆盖一个元素,它会覆盖原始数组等等。当然,一些List
$>
请注意,列表包装器不能在包装器中使用,因为它不能从列表中添加或删除元素。 't extend
ArrayList
- 它是一种不同类型的对象。ArrayList
有自己的内部数组,它们存储自己的元素,并且能够调整内部数组等。包装器没有自己的内部数组, -
另一方面,如果您随后创建一个新数组
new ArrayList< Integer>(Arrays.asList(ia))
然后你创建新的
ArrayList
,这是一个完整的,独立的副本。虽然这里你使用Arrays.asList
创建包装器,它只用于构建新的ArrayList
并随后进行垃圾回收。这个新的ArrayList
的结构完全独立于原始数组。它包含相同的元素(原始数组和这个新的ArrayList
引用内存中的相同整数),但它创建一个新的内部数组,用于保存引用。因此,当您随机播放,添加,删除元素等时,原始数组不变。
what is the difference between
1.List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia)); //copy
2.List<Integer> list2 = Arrays.asList(ia);
where ia
is array of integers.
I came to know that some operations are not allowed in list2
. why is it so?
how is it stored in memory (references / copy)?
When I shuffle the lists, list1
doesn't affect the original array but list2
does. But still list2
is somewhat confusing.
How ArrayList
being upcasted to list differs from creating new ArrayList
list1 differs from (1)
ArrayList<Integer> list1 = new ArrayList<Integer>(Arrays.asList(ia));
First, let's see what this does:
Arrays.asList(ia)
It takes an array
ia
and creates a wrapper that implementsList<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, someList
operations aren't allowed on the wrapper, like adding or removing elements from the list, you can only read or overwrite the elements.Note that the list wrapper doesn't extend
ArrayList
- it's a different kind of object.ArrayList
s 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))
then you create new
ArrayList
, which is a full, independent copy of the original one. Although here you create the wrapper usingArrays.asList
as well, it is used only during the construction of the newArrayList
and is garbage-collected afterwards. The structure of this newArrayList
is completely independent of the original array. It contains the same elements (both the original array and this newArrayList
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.
这篇关于Java中Arrays.asList(array)和new ArrayList之间的区别< Integer>(Arrays.asList(ia))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!