数据结构之解析ArrayList源码的add,remove,set,contains

来源:互联网 发布:显卡内存多大合适知乎 编辑:程序博客网 时间:2024/05/18 19:39

ArrayList的源码的几个重要函数解析
首先,ArrayList内部其实就是一个Object数组构成的,并且需要先说明一下 System.arraycopy这个函数,
System.arraycopy(oldarray, 0, newArray, 0, length);用于数组复制。
oldarray:旧数组
newArray:新数组
第二个参数0:从oldarray数组的第0个元素开始复制
第四个参数0:复制到newArray,从0开始。
第五个参数:复制的长度,

add

add(E object)

   @Override public boolean add(E object) {        Object[] a = array;        int s = size;        if (s == a.length) {            Object[] newArray = new Object[s +                    (s < (MIN_CAPACITY_INCREMENT / 2) ?                     MIN_CAPACITY_INCREMENT : s >> 1)];            System.arraycopy(a, 0, newArray, 0, s);            array = a = newArray;        }        a[s] = object;        size = s + 1;        modCount++;        return true;    }
  • 先是生成一个局部的object[] 数组。ArrayList内部有一个全局变量,来表示数组的长度,size。
  • 内部的元素存储就是array这个全局的object[]数组。
  • 先是对当前的arraylist进行判断,看内部的数组是否有空余,如果没有,则new一个新的数组,
  • 新的数组长度其实就是一个简单的判断,MIN_CAPACITY_INCREMENT为一个常量,在Android中的源码默认为12,在java中长度默认为10。
  • 之后对原来的object进行copy到newArray中。再将新数组的第s个元素赋值为add进来的参数。对size进行+1.表示数组长度进行了改变。 modCount是一个运算计数器。

add(int index, E object)

public void add(int index, E object) {        Object[] a = array;        int s = size;        if (index > s || index < 0) {            throwIndexOutOfBoundsException(index, s);        }        if (s < a.length) {            System.arraycopy(a, index, a, index + 1, s - index);        } else {            // assert s == a.length;            Object[] newArray = new Object[newCapacity(s)];            System.arraycopy(a, 0, newArray, 0, index);            System.arraycopy(a, index, newArray, index + 1, s - index);            array = a = newArray;        }        a[index] = object;        size = s + 1;        modCount++;    }

1.与add(Object)并没有多少区别,如果当arraylist内部的数组长度够用时。对局部数组a进行copy。就是将中间的index多出来一个,后续元素依次往后移,再把object赋值给a[index]。
2.如果内部数组长度不够用时, new一个新的数组,长度为内部的一个函数进行计算后返回的结果.之后对a进行copy。copy两次。第一次copy为从0开始,copy长度为index。第二次为从index+1开始,copy长度为s-index.其实最终目的就是把中间index下标空出来。再将objcet 复制给 a[index]

  private static int newCapacity(int currentCapacity) {        int increment = (currentCapacity < (MIN_CAPACITY_INCREMENT / 2) ?                MIN_CAPACITY_INCREMENT : currentCapacity >> 1);        return currentCapacity + increment;    }

addAll()函数就不具体解释了。实现原理都差不多

remove

remove(int index)

public E remove(int index) {        Object[] a = array;        int s = size;        if (index >= s) {            throwIndexOutOfBoundsException(index, s);        }        @SuppressWarnings("unchecked") E result = (E) a[index];        System.arraycopy(a, index + 1, a, index, --s - index);        a[s] = null;  // Prevent memory leak        size = s;        modCount++;        return result;    }

和add函数实现原理差不多。只是add是将index下标空出来,后续元素依次向后易懂以为。而remove则是将ind。–ex下标的元素去除掉之后,后续元素依次向前移动以为。之后的a[s] =null 是为了防止内存泄漏 .--s - index进行运算后,s本身-1
remove(Object object)

        public boolean remove(Object object) {        Object[] a = array;        int s = size;        if (object != null) {            for (int i = 0; i < s; i++) {                if (object.equals(a[i])) {                    System.arraycopy(a, i + 1, a, i, --s - i);                    a[s] = null;  // Prevent memory leak                    size = s;                    modCount++;                    return true;                }            }        } else {            for (int i = 0; i < s; i++) {                if (a[i] == null) {                    System.arraycopy(a, i + 1, a, i, --s - i);                    a[s] = null;  // Prevent memory leak                    size = s;                    modCount++;                    return true;                }            }        }        return false;    }

remove(object)是对原数组进行遍历,有的话,将所在的元素的删除,之后的元素进行前移一位,与remove(index)没多大区别

set

set(int index, E object)

 @Override public E set(int index, E object) {        Object[] a = array;        if (index >= size) {            throwIndexOutOfBoundsException(index, size);        }        @SuppressWarnings("unchecked") E result = (E) a[index];        a[index] = object;        return result;    }

set函数的源码也不多,就是将原数组的index所在的元素进行替换。其实就是数组的元素进行替换。

contains

contains(Object object)

public boolean contains(Object object) {        Object[] a = array;        int s = size;        if (object != null) {            for (int i = 0; i < s; i++) {                if (object.equals(a[i])) {                    return true;                }            }        } else {            for (int i = 0; i < s; i++) {                if (a[i] == null) {                    return true;                }            }        }        return false;    }

contains源码也不需要多做解释,就是遍历数组,进行判断数组内是否有这个元素

0 0