The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
List
implementations are grouped into general-purpose and special-purpose implementations.实现分为通用和专用实现。
There are two general-purpose 有两种通用List
implementations ArrayList
and LinkedList
. List
实现ArrayList
和LinkedList
。Most of the time, you'll probably use 大多数情况下,您可能会使用ArrayList
, which offers constant-time positional access and is just plain fast. ArrayList
,它提供了恒定时间的位置访问,而且速度非常快。It does not have to allocate a node object for each element in the 它不必为List
, and it can take advantage of System.arraycopy
when it has to move multiple elements at the same time. List
中的每个元素分配一个节点对象,当它必须同时移动多个元素时,它可以利用System.arraycopy
。Think of 将ArrayList
as Vector
without the synchronization overhead.ArrayList
视为没有同步开销的Vector
。
If you frequently add elements to the beginning of the 如果您经常将元素添加到List
or iterate over the List
to delete elements from its interior, you should consider using LinkedList
. List
的开头,或在List
中迭代以从列表内部删除元素,则应考虑使用LinkedList
。These operations require constant-time in a 这些操作需要LinkedList
and linear-time in an ArrayList
. But you pay a big price in performance. LinkedList
中的恒定时间和ArrayList
中的线性时间。但你在表现上付出了巨大的代价。Positional access requires linear-time in a 位置访问在LinkedList
and constant-time in an ArrayList
. LinkedList
中需要线性时间,在ArrayList
中需要恒定时间。Furthermore, the constant factor for 此外,LinkedList
is much worse. LinkedList
的常量因子要差得多。If you think you want to use a 如果您认为要使用LinkedList
, measure the performance of your application with both LinkedList
and ArrayList
before making your choice; ArrayList
is usually faster.LinkedList
,请在做出选择之前,使用LinkedList
和ArrayList
测量应用程序的性能;ArrayList
通常更快。
ArrayList
has one tuning parameter the initial capacity, which refers to the number of elements the 具有一个调谐参数初始容量,指ArrayList
can hold before it has to grow. ArrayList
在必须增长之前可以容纳的元素数。LinkedList
has no tuning parameters and seven optional operations, one of which is 没有调优参数和七个可选操作,其中一个是clone
. clone
。The other six are 其他六个是addFirst
, getFirst
, removeFirst
, addLast
, getLast
, and removeLast
. addFirst
、getFirst
、removeFirst
、addLast
、getLast
和removeLast
。LinkedList
also implements the Queue
interface.LinkedList
还实现了Queue
接口。
CopyOnWriteArrayList
is a 是由写时复制阵列备份的List
implementation backed up by a copy-on-write array. List
实现。This implementation is similar in nature to 此实现在本质上类似于CopyOnWriteArraySet
. CopyOnWriteArraySet
。No synchronization is necessary, even during iteration, and iterators are guaranteed never to throw 即使在迭代期间,也不需要同步,并且保证迭代器永远不会抛出ConcurrentModificationException
. ConcurrentModificationException
。This implementation is well suited to maintaining event-handler lists, in which change is infrequent, and traversal is frequent and potentially time-consuming.这种实现非常适合于维护事件处理程序列表,其中更改很少,遍历频繁且可能耗时。
If you need synchronization, a 如果需要同步,Vector
will be slightly faster than an ArrayList
synchronized with Collections.synchronizedList
. Vector
将略快于与Collections.synchronizedList
同步的ArrayList
。But 但是Vector
has loads of legacy operations, so be careful to always manipulate the Vector
with the List
interface or else you won't be able to replace the implementation at a later time.Vector
有大量的遗留操作,所以要小心始终使用List
接口来操作Vector
,否则以后将无法替换实现。
If your 如果您的List
is fixed in size that is, you'll never use remove
, add
, or any of the bulk operations other than containsAll
you have a third option that's definitely worth considering. List
大小固定也就是说,除了containsAll
,您永远不会使用remove
、add
或任何批量操作;你有第三个选择绝对值得考虑。See 有关详细信息,请参阅方便的实现部分中的Arrays.asList
in the Convenience Implementations section for more information.Arrays.asList
。