Java 集合系列(二)ArrayList详解
程序员文章站
2024-02-24 11:03:22
arraylist
arraylist 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 array 更智能,能够根据集合长度进行自动扩容。...
arraylist
arraylist 是通过一个数组来实现的,因此它是在连续的存储位置存放对象的引用,只不过它比 array 更智能,能够根据集合长度进行自动扩容。
假设让我们来实现一个简单的能够自动扩容的数组,我们最容易想到的点就是:
- add()的时候需要判断当前数组size+1是否等于此时定义的数组大小;
- 若小于直接添加即可;否则,需要先扩容再进行添加。
实际上,arraylist的内部实现原理也是这样子,我们可以来研究分析一下arraylist的源码
add(e e) 源码分析
/** * appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link collection#add}) */ public boolean add(e e) { ensurecapacityinternal(size + 1); // 进行扩容校验 elementdata[size++] = e; // 将值添加到数组后面,并将 size+1 return true; } /** * the array buffer into which the elements of the arraylist are stored. * the capacity of the arraylist is the length of this array buffer. any * empty arraylist with elementdata == defaultcapacity_empty_elementdata * will be expanded to default_capacity when the first element is added. */ transient object[] elementdata; // non-private to simplify nested class access private void ensurecapacityinternal(int mincapacity) { ensureexplicitcapacity(calculatecapacity(elementdata, mincapacity)); // elementdata 数组 } /** * default initial capacity. */ private static final int default_capacity = 10; /** * shared empty array instance used for default sized empty instances. we * distinguish this from empty_elementdata to know how much to inflate when * first element is added. */ private static final object[] defaultcapacity_empty_elementdata = {}; // 返回最大的 index private static int calculatecapacity(object[] elementdata, int mincapacity) { if (elementdata == defaultcapacity_empty_elementdata) { // 与空数组实例对比 return math.max(default_capacity, mincapacity); } return mincapacity; } private void ensureexplicitcapacity(int mincapacity) { modcount++; // overflow-conscious code if (mincapacity - elementdata.length > 0) grow(mincapacity); }
扩容调用方法,实际也就是数组复制的过程
/** * increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param mincapacity the desired minimum capacity */ private void grow(int mincapacity) { // overflow-conscious code int oldcapacity = elementdata.length; int newcapacity = oldcapacity + (oldcapacity >> 1); if (newcapacity - mincapacity < 0) newcapacity = mincapacity; if (newcapacity - max_array_size > 0) newcapacity = hugecapacity(mincapacity); // mincapacity is usually close to size, so this is a win: elementdata = arrays.copyof(elementdata, newcapacity); }
add(int index, e element) 源码分析
/** * inserts the specified element at the specified position in this * list. shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws indexoutofboundsexception {@inheritdoc} */ public void add(int index, e element) { rangecheckforadd(index); // 校验index是否超过当前定义的数组大小范围,超过则抛出 indexoutofboundsexception ensurecapacityinternal(size + 1); // increments modcount!! system.arraycopy(elementdata, index, elementdata, index + 1, size - index); // 复制,向后移动 elementdata[index] = element; size++; } /** * a version of rangecheck used by add and addall. */ private void rangecheckforadd(int index) { if (index > size || index < 0) throw new indexoutofboundsexception(outofboundsmsg(index)); }
从上面的源码分析可知,扩容和随机插入元素的消耗比较大,因此在实际开发中,应尽量指定arraylist大小,减少在随机插入操作。
优缺点
优点
- 封装了一个动态再分配的对象数组
- 使用索引进行随机访问效率高
缺陷
- 在数组中增删一个元素,所有元素都要往后往前移动,效率低下
知识脑图
以上所述是小编给大家介绍的java集合系列arraylist详解整合,希望对大家有所帮助