Java 集合系列(二)—— ArrayList
程序员文章站
2022-07-02 16:36:43
ArrayList ArrayList 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 Array 更智能,能够根据集合长度进行自动扩容。 假设让我们来实现一个简单的能够自动扩容的数组,我们最容易想到的点就是: 实际上,ArrayList的内部实现原理也是这样子,我们可以 ......
arraylist
arraylist 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 array 更智能,能够根据集合长度进行自动扩容。
假设让我们来实现一个简单的能够自动扩容的数组,我们最容易想到的点就是:
- add()的时候需要判断当前数组size+1是否等于此时定义的数组大小;
- 若小于直接添加即可;否则,需要先扩容再进行添加。
实际上,arraylist的内部实现原理也是这样子,我们可以来研究分析一下arraylist的源码
add(e e)
源码分析
1 /** 2 * appends the specified element to the end of this list. 3 * 4 * @param e element to be appended to this list 5 * @return <tt>true</tt> (as specified by {@link collection#add}) 6 */ 7 public boolean add(e e) { 8 ensurecapacityinternal(size + 1); // 进行扩容校验 9 elementdata[size++] = e; // 将值添加到数组后面,并将 size+1 10 return true; 11 } 12 13 14 15 /** 16 * the array buffer into which the elements of the arraylist are stored. 17 * the capacity of the arraylist is the length of this array buffer. any 18 * empty arraylist with elementdata == defaultcapacity_empty_elementdata 19 * will be expanded to default_capacity when the first element is added. 20 */ 21 transient object[] elementdata; // non-private to simplify nested class access 22 23 private void ensurecapacityinternal(int mincapacity) { 24 ensureexplicitcapacity(calculatecapacity(elementdata, mincapacity)); // elementdata 数组 25 } 26 27 28 29 /** 30 * default initial capacity. 31 */ 32 private static final int default_capacity = 10; 33 34 /** 35 * shared empty array instance used for default sized empty instances. we 36 * distinguish this from empty_elementdata to know how much to inflate when 37 * first element is added. 38 */ 39 private static final object[] defaultcapacity_empty_elementdata = {}; 40 41 // 返回最大的 index 42 private static int calculatecapacity(object[] elementdata, int mincapacity) { 43 if (elementdata == defaultcapacity_empty_elementdata) { // 与空数组实例对比 44 return math.max(default_capacity, mincapacity); 45 } 46 return mincapacity; 47 } 48 49 50 51 private void ensureexplicitcapacity(int mincapacity) { 52 modcount++; 53 54 // overflow-conscious code 55 if (mincapacity - elementdata.length > 0) 56 grow(mincapacity); 57 }
扩容调用方法,实际也就是数组复制的过程
1 /** 2 * increases the capacity to ensure that it can hold at least the 3 * number of elements specified by the minimum capacity argument. 4 * 5 * @param mincapacity the desired minimum capacity 6 */ 7 private void grow(int mincapacity) { 8 // overflow-conscious code 9 int oldcapacity = elementdata.length; 10 int newcapacity = oldcapacity + (oldcapacity >> 1); 11 if (newcapacity - mincapacity < 0) 12 newcapacity = mincapacity; 13 if (newcapacity - max_array_size > 0) 14 newcapacity = hugecapacity(mincapacity); 15 // mincapacity is usually close to size, so this is a win: 16 elementdata = arrays.copyof(elementdata, newcapacity); 17 }
add(int index, e element)
源码分析
1 /** 2 * inserts the specified element at the specified position in this 3 * list. shifts the element currently at that position (if any) and 4 * any subsequent elements to the right (adds one to their indices). 5 * 6 * @param index index at which the specified element is to be inserted 7 * @param element element to be inserted 8 * @throws indexoutofboundsexception {@inheritdoc} 9 */ 10 public void add(int index, e element) { 11 rangecheckforadd(index); // 校验index是否超过当前定义的数组大小范围,超过则抛出 indexoutofboundsexception 12 13 ensurecapacityinternal(size + 1); // increments modcount!! 14 system.arraycopy(elementdata, index, elementdata, index + 1, 15 size - index); // 复制,向后移动 16 elementdata[index] = element; 17 size++; 18 } 19 20 21 /** 22 * a version of rangecheck used by add and addall. 23 */ 24 private void rangecheckforadd(int index) { 25 if (index > size || index < 0) 26 throw new indexoutofboundsexception(outofboundsmsg(index)); 27 }
从上面的源码分析可知,扩容和随机插入元素的消耗比较大,因此在实际开发中,应尽量指定arraylist大小,减少在随机插入操作。
优缺点
- 封装了一个动态再分配的对象数组
- 使用索引进行随机访问效率高
- 在数组中增删一个元素,所有元素都要往后往前移动,效率低下
知识脑图
在 github 上建了一个 repository ,java core knowledge tree,各位看官若是喜欢请给个star,以示鼓励,谢谢。
https://github.com/suifeng412/jcktree
(以上是自己的一些见解,若有不足或者错误的地方请各位指出)
作者:
原文地址:
声明:本博客文章为原创,只代表本人在工作学习中某一时间内总结的观点或结论。转载时请在文章页面明显位置给出原文链接
上一篇: oldboy s21day02
下一篇: CTF-变异凯撒