ArrayList作为一个常用的数据容器,不看看源码怎么行呢!!
1.ArrayList的常量:
private transient Object[] elementData;
private int size;
ArrayList核心就是一个Object的数组,这一个数组就解释了ArrayList很多特性,优缺点。
仔细一看,咦有个不怎么常用的关键字transient,来修饰这个数组。(transient加了之后就不会序列化)研究了研究,在网上找到这样一段话:因为elementData里面不是所有的元素都有数据,因为容量的问题,elementData里面有一些元素是空的,这种是没有必要序列化的。ArrayList的序列化和反序列化依赖writeObject和readObject方法来实现。可以避免序列化空的元素。
然后来看看ArrayList的writeObject和readObject。
/**
* Save the state of the <tt>ArrayList</tt> instance to a stream (that
* is, serialize it).
*
* @serialData The length of the array backing the <tt>ArrayList</tt>
* instance is emitted (int), followed by all of its elements
* (each an <tt>Object</tt>) in the proper order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i<size; i++)
s.writeObject(elementData[i]);
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
/**
* Reconstitute the <tt>ArrayList</tt> instance from a stream (that is,
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();
// Read in array length and allocate array
int arrayLength = s.readInt();
Object[] a = elementData = new Object[arrayLength];
// Read in all elements in the proper order.
for (int i=0; i<size; i++)
a[i] = s.readObject();
}
通过流的读写操作排除了空元素的序列化。
第二个常量size 也是经常使用的,这个元素记录着list的长度。
ArrayList自带的判断是否为空的isEmpty方法就是判断的size==0;
2.ArrayList的构造方法:
public ArrayList() {
this(10);
}
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
ArrayList有3个构造方法没传参数的默认数组长度为10,还可以不要默认长度指定ArrayList的长度,还有使用copyOf拷贝一个已有数组方法。
3.ArrayList的增:
add方法
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(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:
elementData = Arrays.copyOf(elementData, newCapacity);
}
add先调用ensureCapacityInternal方法判断是否需要数组扩容,如果不需要扩容直接用elementData[size++] = e赋值。
如果要扩容调用grow方法生成一个1.5倍长度数组,再使用Arrays的copyOf方法把老的数组拷贝到新数组里面。至于Arrays.copyOf方法是怎么执行的也看过,最后是调用的System类里面的arraycopy方法
4.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; // Let gc do its work
return oldValue;
}
删除前用rangeCheck先判断能不能删除,
如果不是最后一个使用System.arraycopy拷贝一个新的list
如果删除的是list的最后一个元素(当list的size==1时),把–size 变成0,再把元素设置成空。
5.ArrayList的修改set方法:
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
set方法就简单很多,先用rangeCheck判断能不能改在直接覆盖当前下标元素。
这个rangeCheck方法单独提一下,抛出下标越界异常的方法就是它
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}