为什么Arrays.asList()返回了自己的ArrayList实现 [英] Why does Arrays.asList() return its own ArrayList implementation

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

问题描述

我最近发现,居然有在Java 2个不同的的ArrayList 实现(总好过从来没有我猜...)。

I recently found out that there are actually 2 different ArrayList implementations in Java (better late than never I guess...).

所以我想知道为什么 Arrays.asList(T ...一)需要返回不能被调整名单?如果他们需要一个不可修改的列表,为什么添加设置(INT指数,E元素)方法呢?

So I was wondering why does Arrays.asList(T... a) need to return a list which can not be resized ? If they needed an unmodifiable list why add the set(int index, E element) method then ?

所以,我一般的问题是,为什么不返回的java.util.ArrayList Arrays.asList(T ...一)的方法?

So my general question is why not return the java.util.ArrayList from the Arrays.asList(T... a) method ?

另外你怎么用 java.util.Arrays.ArrayList 实施获得?

Also what do you gain with the java.util.Arrays.ArrayList implementation ?

推荐答案

您问:

另外你怎么用增益
  java.util.Arrays.ArrayList
  执行?

Also what do you gain with the java.util.Arrays.ArrayList implementation ?

这是因为通过Arrays.asList返回数组$ ArrayList是只是原来阵列上的看法。所以,当原始数组被改变,那么该视图过改变。

It is because the Arrays$ArrayList returned by Arrays.asList is just a view on the original array. So when the original array is changed then the view is changed too.

如果人会使用一个真正的ArrayList那么元素将被复制,并还原最初阵列上的变化不会影响,积极挖掘该ArrayList

If one would use an real ArrayList then the elements will be copied, and a change on the orignal array would not infuence the ArrayList.

的原因要做到这一点是非常简单的:

The reasons to do this are quite simple:


  • 性能:无需复制点儿

  • 内存efficent:不需要第二个数组

这篇关于为什么Arrays.asList()返回了自己的ArrayList实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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