@@ -115,7 +115,9 @@ List list = Arrays.asList(1, 2, 3);
115115
116116### 1. 概览
117117
118- 因为 ArrayList 是基于数组实现的,所以支持快速随机访问。RandomAccess 接口标识着该类支持快速随机访问。
118+ 因为 ArrayList 是基于数组实现的,所以支持快速随机访问(可以使用下标来访问,随机指的是下标是随机的 get(int index))。RandomAccess 接口标识着该类支持快速随机访问。
119+
120+ ArrayList类中存在两个私有成员(当然不仅仅只有两个),elementData是一个Object类型的数组,用来保存元素;size表示ArrayList中实际的元素数目(所以size()方法返回的是实际元素的数目)。
119121
120122``` java
121123public class ArrayList <E> extends AbstractList<E >
@@ -126,6 +128,10 @@ public class ArrayList<E> extends AbstractList<E>
126128
127129```java
128130private static final int DEFAULT_CAPACITY = 10;
131+
132+ public ArrayList () {
133+ this (10 );
134+ }
129135```
130136
131137<div align =" center " > <img src =" pics/52a7744f-5bce-4ff3-a6f0-8449334d9f3d.png " width =" 400px " > </div ><br >
@@ -170,6 +176,9 @@ private void grow(int minCapacity) {
170176}
171177```
172178
179+ 看了这几段代码,ensureCapacity(minCapacity)的作用就显而易见了,就是增加数组列表的容量,原理很简单,首先判断该参数是否大于0并且是否大于当前容量,然后就到了grow方法,其他的语句先别管,看最后一句,elementData = Arrays.copyOf(elementData, newCapacity); 这条语句先按指定参数创建一个更大的数组,然后把原数组的元素一个个拷贝到大数组中,最后将elementData 的引用指向这个大数组。这样,就增大了数组列表的容量,而小数组也会被gc回收。所以数据量很大的时候还是建议初始化的时候就指定容量,提高效率
180+
181+
173182### 3. 删除元素
174183
175184需要调用 System.arraycopy() 将 index+1 后面的元素都复制到 index 位置上,该操作的时间复杂度为 O(N),可以看出 ArrayList 删除元素的代价是非常高的。
@@ -545,7 +554,7 @@ private V putForNullKey(V value) {
545554``` java
546555void addEntry(int hash, K key, V value, int bucketIndex) {
547556 if ((size >= threshold) && (null != table[bucketIndex])) {
548- resize(2 * table. length);
557+ resize(2 * table. length); // resize的时候double size
549558 hash = (null != key) ? hash(key) : 0 ;
550559 bucketIndex = indexFor(hash, table. length);
551560 }
@@ -774,7 +783,7 @@ static final int tableSizeFor(int cap) {
774783### 9. 与 HashTable 的比较
775784
776785- HashTable 使用 synchronized 来进行同步。
777- - HashMap 可以插入键为 null 的 Entry。
786+ - HashMap 可以插入键为 null 的 Entry,HashTable不可以 。
778787- HashMap 的迭代器是 fail-fast 迭代器。
779788- HashMap 不能保证随着时间的推移 Map 中的元素次序是不变的。
780789
0 commit comments