arrayList的实现

来源:互联网 发布:淘宝不发货会自动退款 编辑:程序博客网 时间:2024/05/22 18:21

要想看arrayList的数据结构,还是要看arrayList的源码中的插入,删除方法

   添加

public boolean add(E e) {
    //涉及到扩容,了解当前元素的个数
    ensureCapacityInternal(size + 1);  // Increments modCount!!
    //添加元素,也能看出来是数组的添加方式,其实arraylist就是动态数组    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++;    //显然大于当前容量要扩容    if (minCapacity - elementData.length > 0)        grow(minCapacity);}
private void grow(int minCapacity) {    // overflow-conscious code    int oldCapacity = elementData.length;
//特别注意这里这是扩大了多少呢?
 如果oldCapacity 初始值是10
就是  10+ (10 >> 1) 将10右移1位
那么10的2进制0000 0000 0000 0000 0000 0000 0000 1010  去掉一位 变 0000 0000 0000 0000 0000 0000 0000 0101 变成5
故 变为容量变为15
   数学意义:右移一位相当于除2,右移n位相当于除以2的n次方。
 int newCapacity = oldCapacity + (oldCapacity >> 1);
转换下就是  int newCapacity =1+oldCapacity/2 
    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:  
//将旧的数组copy到新的数组中,扩容就结束了
  elementData = Arrays.copyOf(elementData, newCapacity);
}
//array的copy部分
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;}
会创建一个数组,数组的大小事新数组要扩容的大小,然后执行copy的操作
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
既然是动态数组,肯定能制定添加位置的
public E set(int index, E element) {    rangeCheck(index);    E oldValue = elementData(index);    elementData[index] = element;    return oldValue;}
private void rangeCheck(int index) {    if (index >= size)        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));}

很简单,就是有值就替换成新值,并且这个函数不能直接使用,必须原位置上有值
小测试
public class ColandMap {    public static void main(String[] args) {   ArrayList<String> a=new ArrayList<String>();        System.out.println(a.add("111"));        System.out.println(a.set(0,"222"));        System.out.println(a.get(0));    }}
结果:
true111222
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
我现在很好奇那ArrayList的删除操作如何实现
public E remove(int index) {    rangeCheck(index);    modCount++;    E oldValue = elementData(index);    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    return oldValue;}


        

原创粉丝点击