您的位置:首页 > 编程语言 > Java开发

Java ArrayList源码小结

2017-02-10 23:03 447 查看
ArrayList是Java集合框架中的动态数组:

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable

ArrayList实现了List接口,实现了RandomAccess(随机访问)、Serializable(序列化);

RandomAccess接口是一个标记接口,实现了RandomAccess接口的集合类,使用for(int i=0;i<list.size();i++) {list.get(i);}的效率要高于使用增强for循环遍历集合;

private static final int DEFAULT_CAPACITY = 10;

ArrayList底层由数组实现,默认容量为10;

private int size;


底层数组的长度,注意与Capacity区分;

构造函数有:public ArrayList(int initialCapacity),自定义底层数组大小;

                      public ArrayList(),默认数组大小;

                      public ArrayList(Collection<? extends E> c),复制构造方法;

public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = (size == 0)
? EMPTY_ELEMENTDATA
: Arrays.copyOf(elementData, size);
}
}去除预留元素的空间,使capacity=size;

ArrayList通过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);
}如果数组现有长度的1.5倍大于minCapacity,则扩容至现长度的1.5倍,反之则扩容至minCapacity的大小;

public int size() {
return size;
}得到底层数组的长度,时间复杂度为常数级别;

public boolean isEmpty() {
return size == 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;
}
得到o对象第一次出现的下标,o对象可以为空,比较时采用的是equals方法,若没有找到相等的对象,返回-1;

public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}反着搜索;

public boolean contains(Object o) {
return indexOf(o) >= 0;
}调用indexOf方法,判断集合中是否有o对象;

public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
拷贝ArrayList的底层数组并返回,返回数组的长度与底层数组的size一致;

public E get(int index) {
rangeCheck(index);

return elementData(index);
}
get方法先做安全性检查rangeCheck再得到index位置上的值;

public E set(int index, E element) {
rangeCheck(index);

E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}set方法先rangeCheck再修改index位置上的值;

我们再来看rangeCheck方法:
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}注释上说,这个方法不检查index是否为负值?

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

ensureCapacityInternal(size + 1);  // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
第一个add方法将元素添加到数组末尾;第二个add方法调用System.arraycopy将elementData从index开始的size-index个元素复制到index+1至size+1的位置(即index开始的元素都向后移动一个位置),然后将element插入到index位置;

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;
}
删除某一位置元素,调用System.arraycopy将elementData从index+1开始的numMoved个元素复制到index为开头的位置;返回删除的元素的值;

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
}fastRemove方法:不做下标检查,直接删除,也没有返回值;

public boolean remove(Object o) {
if (o == 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])) {
fastRemove(index);
return true;
}
}
return false;
}先搜索,再调用fastRemove方法;因为已经判定index值有效,可以跳过下标检查,采用fastRemove方法;

public void clear() {
modCount++;

// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;

size = 0;
}
将数组元素全部指向null,等待GC;

public boolean addAll(Collection<? extends E> 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 addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);

Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);  // Increments modCount

int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);

System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
先将集合c转换成数组,再添加或插入,并且更新size;

protected void
4000
removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex,
numMoved);

// clear to let GC do its work
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}

删除数组的一部分,并将元素指向null;

参考博客:http://blog.csdn.net/jzhf2012/article/details/8540410
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: