Java8 ArrayList的实现,java8arraylist
分享于 点击 12775 次 点评:31
Java8 ArrayList的实现,java8arraylist
ArrayList底层是使用数组的方式实现的,在java8中初始化则是分配一个内存为0的对象数组(常量类型),在添加时在不断分配新内存
- 初始化
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
//默认的数组长度
private static final int DEFAULT_CAPACITY = 10;
//java8以后的ArrayList初始化之后并不会马上分配内存空间
private static final Object[] EMPTY_ELEMENTDATA = {};
//transient修饰非序列化
transient Object[] elementData; // non-private to simplify nested class access
private int size;
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
}
- 添加节点
public boolean add(E e) {
//判断是否需要扩容(传入的参数是目前所需要的最小数组长度)
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
//判断默认的长度是否符合要求
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// 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:
//Arrays.copyOf(oldArray,newArrayLength)
elementData = Arrays.copyOf(elementData, newCapacity);
}
注意:Arrays.copyOf(elementData, newCapacity)在底层视线中非常常见,是将数组有一个数组复制到另一个数组中
区别:System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length)表示将数组src从srcPost的位置开始复制,复制的长度为length,复制到dest数组的destPost位置上
- 删除节点(给出节点位置)
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;
}
//判断节点位置是否存在
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
- 删除节点(已知节点内容)
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;
}
//快速删除节点
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
}
同样的,在ArrayList中允许节点内容为空,也允许节点内容重复
- 获取某位置的节点
public E get(int index) {
rangeCheck(index);
checkForComodification();
return ArrayList.this.elementData(offset + index);
}
//判断该节点是否已经被修改了(这个一般用于并发操作的控制,防止多个线程都写不一致)
private void checkForComodification() {
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}
- 设置某位置的节点为指定内容
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
//获取某位置的节点
E elementData(int index) {
return (E) elementData[index];
}
- 获取表长度
//同样这里增强了对并发操作的版本控制,查看目前的size是否被改变过
public int size() {
checkForComodification();
return this.size;
}
相关文章
- 暂无相关文章
用户点评