问题描述
方法返回表示 A
。
此处返回的对象是,但不是 ArrayList
对象。
The method Arrays.asList(<T>...A)
returns a List
representation of A
.The returned object here is a List
backed by an array, but is not an ArrayList
object.
我正在寻找对象返回并且对象 - 一个快速的来源,告诉这些而不深入代码。
I'm looking for the differences between the object Arrays.asList()
returns and an ArrayList
object-- a quick source to tell these without diving into the code.
TIA。
推荐答案
当你调用Arrays.asList时,它不会返回 java.util.ArrayList
。它返回 java.util.Arrays $ ArrayList
,这是一个不可变列表。您无法添加它,也无法从中删除。
When you call Arrays.asList it does not return a java.util.ArrayList
. It returns a java.util.Arrays$ArrayList
which is an immutable list. You cannot add to it and you cannot remove from it.
如果您尝试添加或删除元素,您将获得 UnsupportedOperationException
If you try to add or remove elements from them you will get UnsupportedOperationException
这篇关于ArrayList与Arrays.asList()返回的List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!