ArrayList与Arrays.asList()返回的List [英] ArrayList vs the List returned by Arrays.asList()

查看:330
本文介绍了ArrayList与Arrays.asList()返回的List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

方法 Arrays.asList(< T> ... A) 返回 列表 表示 A
此处返回的对象是 <$ c由数组支持的$ c> List ,但不是 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.

我正在寻找对象 Arrays.asList() 返回并且 ArrayList 对象 - 一个快速的来源,告诉这些而不深入代码。

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

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