JDK 源码解读之 ArrayList
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK 源码解读之 ArrayList相关的知识,希望对你有一定的参考价值。
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
文档中提到:size isEmpty get set iterator listIterator 操作,是( run in constant time), add操作是(run in amortized constant time),而 其他的操作是 (run in linear time).
Constant time means the operation doesn‘t depend on the length of the input. Linear time means the operation grows linearly with the change in the length of the input.
就想比较 两个(4bytes int a ,b)的大小是 constant time,并不会因为 a,b的大小而不同。用大O表示法就是:O(1)。而 找出一个数组中 最大的 值,就是Linear time,因为时间随着 数组中元素的增加而增加。用大O表示法就是:O(n)。
什么是amortized constant time呢?
StackOverFlow上是这么说的:
If you do an operation say a million times, you don‘t really care about the worst-case or the best-case of that operation - what you care about is how much time is taken in total when you repeat the operation a million times.
Essentially amortised time means "average time taken per operation, if you do many operations". Amortised time doesn‘t have to be constant; you can have linear and logarithmic amortised time or whatever else.
以上是关于JDK 源码解读之 ArrayList的主要内容,如果未能解决你的问题,请参考以下文章