为什么不总是在Java中使用ArrayLists而不是普通的'数组? [英] Why not always use ArrayLists in Java, instead of plain ol' arrays?

查看:297
本文介绍了为什么不总是在Java中使用ArrayLists而不是普通的'数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问:为什么不总是在Java中使用ArrayLists?除了额外的有用功能之外,它们显然具有与阵列相同的访问速度。我理解它不能保存原语的限制,但这可以通过使用包装器轻松减轻。

Quick question here: why not ALWAYS use ArrayLists in Java? They apparently have equal access speed as arrays, in addition to extra useful functionality. I understand the limitation in that it cannot hold primitives, but this is easily mitigated by use of wrappers.

推荐答案

如果你需要一个原始的集合,然后一个数组可能是这项工作的最佳工具。拳击是一项相对昂贵的手术。对于将用作基元的基元的集合(不包括地图),我几乎总是使用数组来避免重复装箱和拆箱。

If you need a collection of primitives, then an array may well be the best tool for the job. Boxing is a comparatively expensive operation. For a collection (not including maps) of primitives that will be used as primitives, I almost always use an array to avoid repeated boxing and unboxing.

我很少担心但是,数组与 ArrayList 之间的性能差异。如果 List 将提供更好,更清晰,更易维护的代码,那么我将始终使用 List (或 Collection 设置等,但你的问题是关于 ArrayList )除非有一些令人信服的理由不这样做。性能很少令人信服的原因。

I rarely worry about the performance difference between an array and an ArrayList, however. If a List will provide better, cleaner, more maintainable code, then I will always use a List (or Collection or Set, etc, as appropriate, but your question was about ArrayList) unless there is some compelling reason not to. Performance is rarely that compelling reason.

使用集合几乎总能带来更好的结果代码,部分是因为数组不能与泛型一起使用,正如JohannesWeiß在评论中已经指出的那样,还因为有很多其他原因:

Using Collections almost always results in better code, in part because arrays don't play nice with generics, as Johannes Weiß already pointed out in a comment, but also because of so many other reasons:


  • 集合有一个非常丰富的API和各种各样的实现,可以(在大多数情况下)互相交换进来。

  • 集合可以简单地转换偶然使用数组版本是有用的

  • 许多集合比数组增长更优雅,这可能是性能问题
  • >
  • 集合非常适用于泛型,数组非常糟糕

  • 正如TofuBeer指出的那样,数组协方差很奇怪,并且可以以无法控制的方式操作,没有任何对象会在。集合以预期的方式处理协方差。

  • 数组需要手动siz编辑他们的任务,如果数组不满,你需要自己跟踪。如果需要调整数组的大小,你必须自己做。

  • Collections have a very rich API and a large variety of implementations that can (in most cases) be trivially swapped in and out for each other
  • A Collection can be trivially converted to an array, if occasional use of an array version is useful
  • Many Collections grow more gracefully than an array grows, which can be a performance concern
  • Collections work very well with generics, arrays fairly badly
  • As TofuBeer pointed out, array covariance is strange and can act in unexected ways that no object will act in. Collections handle covariance in expected ways.
  • arrays need to be manually sized to their task, and if an array is not full you need to keep track of that yourself. If an array needs to be resized, you have to do that yourself.

所有这些在一起,我很少使用数组,只有一个更多时候使用 ArrayList 。但是,我经常使用 List (或只是 Collection Set )。我最常使用的数组是当存储的项目是一个基元时,它将被插入并访问并用作基元。如果拳击和拆箱都变得如此之快以至于变得微不足道,我可能会重新审视这个决定,但是以一种始终引用的形式处理某些东西,存储它会更方便。 (即'int'而不是'Integer'。)

All of this together, I rarely use arrays and only a little more often use an ArrayList. However, I do use Lists very often (or just Collection or Set). My most frequent use of arrays is when the item being stored is a primitive and will be inserted and accessed and used as a primitive. If boxing and unboxing every become so fast that it becomes a trivial consideration, I may revisit this decision, but it is more convenient to work with something, to store it, in the form in which it is always referenced. (That is, 'int' instead of 'Integer'.)

这篇关于为什么不总是在Java中使用ArrayLists而不是普通的'数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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