java学习笔记(2)-ArrayList类

1.为了方便查看我把源码几个方法和变量复制出来了

package com.itao.study.study2;

public class ArrayList {
	
	private static final int DEFAULT_CAPACITY = 10;
	private static final Object[] EMPTY_ELEMENTDATA = {};
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

	transient Object[] elementData;
	
    public ArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        }
    }
 
    public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

}

定义了默认的容量:DEFAULT_CAPACITY=10;

通常我们在代码中会这样使用     List list=new ArrayList();   其实就是复制对象数组elementData为空数组


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);  //Math.max()返回两个参数的最大值
        }

        ensureExplicitCapacity(minCapacity);
    }
private void ensureExplicitCapacity(int minCapacity) {
        modCount++;	//这里的modCount在AbstractList中定义的,值为0


        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }


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);
    }

在添加元素的时候,注意这里的size初始值是0,因为是类变量,在类初始化的时候jvm自动赋值为0

添加的时候数组长度增长,当对象数组长度大于默认的10时,数组会进行扩容,扩容的值为:原来数组的长度size  +  (size>>1)

比如原来List中已有10个数据,在此添加则扩容为10+ 10>>1 =15


从上面的源码也可以看出:size,elementData根本没进行同步,也就是说ArrayList是线程不安全的


public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(size + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                         size - index);	   //复值数组的后半部分,从index开始移到index+1开始,index位置添加element
        elementData[index] = element;
        size++;
    }

private void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

public boolean addAll(Collection c) {
        Object[] a = c.toArray();
        int numNew = a.length;
        ensureCapacityInternal(size + numNew);  // Increments modCount
        System.arraycopy(a, 0, elementData, size, numNew);
        size += numNew;
        return numNew != 0;
    }

这几个方法也很简单,主要就是调用系统数组拷贝函数,关键点在复值原数组和目的数组的起始位置


public boolean contains(Object o) {
        return indexOf(o) >= 0; 
    }

public int indexOf(Object o) {
        if (o == null) {
            for (int i = 0; i < size; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = 0; i < size; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

arrayList.contain(Object o) 其实就是遍历数组,来比较的


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;
    }

E elementData(int index) {
        return (E) elementData[index];
    }

remove(int index)方法就是把index位置的元素置为null而已


你可能感兴趣的:(java学习笔记(2)-ArrayList类)