ArrayList
来源:互联网 发布:天纵国际软件 编辑:程序博客网 时间:2024/06/08 20:17
初始化过程
构造器
/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } transient Object[] elementData; // non-private to simplify nested class access private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
可见默认ArrayList底层是数组结构,初始化时构建一个空数组,DEFAULTCAPACITY_EMPTY_ELEMENTDATA由static修饰,可以看成是存在方法区的缓存数组,可以避免new时创建无数的无用数组。
元素增删
扩容与add()方法
private int size; //The size of the ArrayList (the number of elements it contains). private static final int DEFAULT_CAPACITY = 10; /** * Appends the specified element to the end of this list. */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; //数组初始化后添加元素,并让长度自增 return true; } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } //如果底层数组是默认数组就取两个参数的大值 ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; //修改次数 // overflow-conscious code if (minCapacity - elementData.length > 0) //判断参数是否大于底层数组长度 grow(minCapacity); //扩容方法 } private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; /** * 计算扩容后的数组长度, 然后调用copyOf()方法 */ private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); //1.5倍扩容 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); } //数组复制方法 public static <T> T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass()); } public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] //创建一个数组,长度由之前方法传入 : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); //复制数组的本地方法 return copy; }
总结一下ArrayList添加过程:
1.设置新长度为size + 1。
2.如果底层是默认缓存数组,设置新长度值为默认长度。
3.如果新长度值大于底层数组长度,调用扩容方法,modCount++。
4.计算扩容后的数组长度。
5.创建一个新数组,并复制原数组的数据,返回新数组。(通常扩容1.5倍)
创建ArrayList时选取合适的初始大小可以减少扩容次数,减少底层数组的复制次数,从而提高性能。
删除元素remove()方法
ArrayList删除某个元素时会把它后面的元素复制到前一格去,然后删除掉最后一节点的元素使其为null。
是复制不是移动,因此复制完成后最后两个元素指向同一个对象。
最后一节点的元素由于失去引用,会在下次GC到来时被JVM回收。
这样可以避免ArrayList中间有一格null元素。
因此,过多的删除操作会造成数组的大量复制工作,时间复杂度为O(n),此时应考虑使用LinkedList链表结构。
根据下标删除
public E remove(int index) { rangeCheck(index); //检查下标是否越界并报错的方法,不代码贴了。 modCount++; E oldValue = elementData(index); int numMoved = size - index -1; //要移动的元素数量 if (numMoved > 0) System.arraycopu(elementData, index + 1, elementData, index, numMoved); elementData[--size] = null //最后一个元素置空 return oldValue;}
根据元素Object删除
public boolean remove(Object o) { if(o == null) { //要删除的元素为null for (int index = 0; index < size; index++) if (elementData[index] == null) { fastRemove(index); return true; } } else { for (int index = 0; index < size; index++) if (o.equals(elementData[index])) { //调用了要删除元素的equals()方法 fastRemove(index); return true; } } return false; } /* * Private remove method that skips bounds checking and does not * return the value removed. */ private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work }
可以看到根据元素Object来删除ArrayList里的元素时会调用要删除元素的equals( )方法,因此,在ArrayList里存储对象时可能需要重写equals( )来使业务逻辑正确执行。(不重写时equals默认使用继承自Object类的 == 逻辑) 而重写equals( )后又要重写hashCode方法,所以不建议使用这个方法。
元素读取
public E get(int index) { rangeCheck(index); return elementData(index); } public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
读/取方法比较简单。
复杂度
可见ArrayList的元素读/取都是通过底层数组下标直接定位的,复杂度O(1)。
增删操作在不触发数组复制时复杂度O(1),否则为O(n)。
会造成底层数组复制的操作:
-增:扩容(添加元素时底层数组长度不够)
-删:不是最后一个元素的删除操作
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- ArrayList
- Qt程序打包发布方法(使用官方提供的windeployqt工具)
- IntelliJ Idea 常用快捷键
- 从零开始前端学习[12]:css样式中的高级选择器
- 词汇表
- Mybatis学习笔记--SqlSessionFactory、SqlSession等
- ArrayList
- input、put、input() 、put()用法与区别
- java 设计模式 装饰模式
- char a[](字符串数组)和char *a(字符串指针)区别
- 动态添加Fragment
- Qt5 程序发布打包
- WPF数据绑定机制是如何实现
- request对象
- 两种高性能I/O设计模式(Reactor/Proactor)的比较