java.util.Arrays中的私有静态类ArrayList - 为什么? [英] private static class ArrayList in java.util.Arrays - Why?

查看:142
本文介绍了java.util.Arrays中的私有静态类ArrayList - 为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在java.util.Arrays中,定​​义了一个名为ArrayList的私有静态类。
它仅从Arrays.asList方法引用。

In java.util.Arrays there is a private static class called "ArrayList" defined. It is only referred from Arrays.asList method.

这样做有什么好处?
为什么不改为引用java.util.ArrayList?

What is the benifit of doing this? Why is java.util.ArrayList not referred instead?

以下代码:

   /**
    * @serial include
    */
      private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable


推荐答案


这样做有什么好处?为什么不引用 java.util.ArrayList

一个原因是实际的实现类不是公共API细节。这样做意味着他们可以在将来更改它的实现类...没有破坏客户代码的任何风险。

One reason is that the actual implementation class is not a public API detail. Doing this means that they can change the implementation class it in the future ... without any risk of breaking customer code.

这样做的另一个原因是这个私有类以不同于ArrayList的方式实现某些操作。特别是涉及更改列表大小的操作需要实现以抛出异常...以符合javadocs中为指定的行为Arrays.asList(...) 方法。

Another reason for doing this is that this private class implements some operations differently to ArrayList. In particular operations that would involve changing the size of the list need to be implemented to throw an exception ... in order to conform to the behaviour specified in the javadocs for the Arrays.asList(...) method.

实际上, Arrays.asList(...)是原始数组的包装器,而不是完整的函数列表。这有利有弊:

In reality, the list returned by Arrays.asList(...) is a wrapper for the original array, and not a full function list. This is has advantages and disadvantages:


  • 在不利方面,某些操作不起作用。

  • On the down-side, certain operations don't work.

从好的方面来说,创建一个包装器要比从一个数组中创建一流列表要便宜得多。 (后者需要将数组内容复制到列表中......对于大型数组而言,这将是昂贵的。)

On the up-side, creating a wrapper is a lot cheaper than creating a first-class list out of an array. (The latter entails copying the array contents into the list ... and that will be expensive for a large array.)

此外,还存在这样的问题:通过包装器可以看到对原始数组的更改(反之亦然)......如果这是您需要的,这可能很有用。

Also, there is the issue that changes to the original array are visible via the wrapper (and vice versa) ... which can be useful if that is what you need.

您在评论中提到了这一点:

You asked this in a comment:


a)为什么要返回不可调整大小的列表?

a) Why return non resizable list?

因为返回一个常规的可调整大小的列表需要在某些时候复制数组内容......这很昂贵。 (如果实现推迟了复制,直到执行了大小改变操作,原始数组和列表之间的关系将很难理解。想一想......)

Because returning a regular resizable list entails copying the array contents at some point... which is expensive. (And if the implementation deferred the copying until a size-changing operation was performed, the relationship between the original array and the list would be really difficult to understand. Think about it ...)


b)为什么不使用Collections.unmodifiableList并传递java.util.ArrayList对象?

b) Why not use Collections.unmodifiableList and pass the java.util.ArrayList object?

这没有任何成就。您仍然必须将数组内容复制到 ArrayList 。这个奇怪的行为规范的重点是避免需要复制。

That doesn't achieve anything. You still have to copy the array contents to the ArrayList. The whole point of this "strange" behavioural spec is to avoid the need to copy.

这篇关于java.util.Arrays中的私有静态类ArrayList - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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