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

查看:35
本文介绍了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 不同的操作.在涉及更改列表大小的特定操作中,需要实现以抛出异常...以符合 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天全站免登陆