您的位置:首页 > 其它

集合之ArrayList实现源码分析

2017-09-21 14:33 417 查看
ArrayList在开发过程中用的还是比较普遍的,接下来从它的构造方法,add()、indexOf()、iterator()等方法来解析它的实现。

一构造方法

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

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}

/**
* Constructs a list containing the elements of the specified
* collection, in the order they are returned by the collection's
* iterator.
*
* @param c the collection whose elements are to be placed into this list
* @throws NullPointerException if the specified collection is null
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}


ArrayList提供了以上三种初始化的方式,第一种参数为初始化数组的大小,并且要求initialCapacity是大于0的;第二种没有参数,直接将elementData赋值为空数组;第三种参数为Collection类型,判断Collection是否为空来初始化elementData的值。以上提到的elementData是数组类型,为ArrayLsit的成员变量,接下来对ArrayList方法的调用实际上都是数组elementData来实现的,也就是说本质上还是对数组的增删改查。

add()方法

public boolean add(E e) {
ensureCapacityInternal(size + 1);  // Increments modCount!!
elementData[size++] = e;
return true;
}


假设初始化的时候没有传递任何参数,size的初始值为0,由上面的分析知道,这时候elementData是一个空的数组,所以ensureCapacityInternal()方法中一定会执行elementData的初始化操作,接着往下看:

private void ensureCapacityInternal(int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}

ensureExplicitCapacity(minCapacity);
}


这个方法中的DEFAULTCAPACITY_EMPTY_ELEMENTDATA也是一个空数组,而DEFAULT_CAPACITY的值为10,所以这时候的minCapacity值为10,接着往下:

private void ensureExplicitCapacity(int minCapacity) {
modCount++; // 记录直接modify该集合的次数

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


同样接着往下看grow()方法:

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


在grow()方法中调用了Arrays.copyOf()来实现elementData的初始化,继续向下跟进:

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


最后调用的是System.arrayCopy()方法,该方法是一个native方法,会成功返回一个大小的数组。grow()方法会根据当前集合size的大小来调整数组的长度,每次变为原来的1.5倍。

indexOf()方法

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


该方法就是遍历集合,查找是否有相等的元素,有则返回对应的角标。

iterator()

public Iterator<E> iterator() {
return new Itr();
}


private class Itr implements Iterator<E> {
int cursor;       // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
int expectedModCount = modCount;

public boolean hasNext() {
return cursor != size;
}

@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}

public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();

try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}

@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}

final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}


里面一些方法的介绍已经在上一篇ArrayList之ConcurrentModificationException异常源码分析介绍过了,这里就不在叙述。总的来说,ArrayList的实现还是比较简单的,里面的一些方法也主要是针对数组的一些操作。值得注意的地方是,在删除集合元素的时候,只是将对应数组中的元素设置为null,并没有改变数组的大小,只是将ArrayList的成员变量size的大小进行改变。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  arraylist iterator 对象