ArrayList实现接口与继承
一.为什么储存内容的 Object[] elementData 是 transient
ArrayList 的大小是动态拓展的,也就是说可能出现容量(capacity)大小为10 ,但是内容大小(size)为5个,那么就没有必要按照容量大小序列化,
所以ArrayList自己实现了 writeObject和readObject,通过writeObject自己实现了elementData 序列化,具体如下:
private void writeObject ( java. io. ObjectOutputStream s)
throws java. io. IOException{
int expectedModCount = modCount;
s. defaultWriteObject ( ) ;
s. writeInt ( size) ;
for ( int i= 0 ; i< size; i++ ) {
s. writeObject ( elementData[ i] ) ;
}
if ( modCount != expectedModCount) {
throw new ConcurrentModificationException ( ) ;
}
}
关于writeObject和readObject可参考这篇文章(转载):
[什么是writeObject 和readObject?可定制的序列化过程](https://www.iteye.com/blog/bluepopopo-486548)
二.初始化过程和扩容
1.新创建并且未指定初始化容量大小ArrayList中声明了2个内容为空的Object数组,如下:
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
DEFAULTCAPACITY_EMPTY_ELEMENTDATA 这个数组会在首次添加元素的时候用到:
/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
根据源码可以看到,每次使用add(E e) 这个方法,都会调用ensureCapacityInternal(size + 1) 方法,跟进这个方法我们可以看到ArrayList的初始化容量和动态拓展的实现,无参构造方法中是使用DEFAULTCAPACITY_EMPTY_ELEMENTDATA 这个空数组,
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
此方法可以看到,如果 elementData 是DEFAULTCAPACITY_EMPTY_ELEMENTDATA,则会返回
return Math.max(DEFAULT_CAPACITY, minCapacity) 中的较大的值,如果是无参构造方法创建的对象,则可以判断初始化容量为:10,每次添加元素的时,都会通过:
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// 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);
}
根据 int newCapacity = oldCapacity + (oldCapacity >> 1);我们可以知道ArrayList每次扩容大小是原来的一半。
三.SubList成员内部类
ArrayList中包含一个名为:subList 的成员内部类,调用:
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
其实是对当前对象的引用,
SubList(AbstractList<E> parent,
int offset, int fromIndex, int toIndex) {
this.parent = parent;
this.parentOffset = fromIndex;
this.offset = offset + fromIndex;
this.size = toIndex - fromIndex;
this.modCount = ArrayList.this.modCount;
}
测试代码:
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("马六");
List<String> list1 = list.subList(0,2);
list1.add("12");
System.out.println(list.getClass().getName());
System.out.println(list1.getClass().getName());
System.out.println(list1.get(0));
System.out.println(list.toString());
System.out.println(list1.toString());
}
输出结果:
java.util.ArrayList
java.util.ArrayList$SubList
张三
[张三, 李四, 12, 王五, 马六]
[张三, 李四, 12]
debug System.out.println(list1.get(0)); 可以发现get 方法调用的是sublist中的get方法:
public E get(int index) {
rangeCheck(index);
checkForComodification();
return ArrayList.this.elementData(offset + index);
}
可以看到获取的值是当前对象的elementData中的元素