ArrayList 源代码笔记

来源:互联网 发布:剑网三梨花叹副本数据 编辑:程序博客网 时间:2024/05/18 01:42

ArrayList的初始化

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);    }
从这个构造函数可以看到,传入的集合对象的类型会被还原到Object[].class


ArrayList扩容

ArrayList的默认容量为10,方法ensureCaptacity(int c)可以增加容量

如果c比原来容量的1.5倍要小,扩容为1.5倍,
否则扩容为c


ensureCapacity方法最后会生成一个新容量的Array,并使用Arrays.copyOf的方法将原有的数据复制到新的数组

ArrayList内部使用int size来保存现有元素的数量


当使用ArrayList(Collection<? extends E c> c)这个构造函数时,size被初始化为c.toArray().length


trimToSize(),当现有的元素数小于数组的容量时,根据现有的元素数目生成一个新数组

代价:每次使用该方法都需要进行一次数组的复制,花费 O(n)真是不菲啊。。。


对指定index进行插入添加时,先确保容量,然后对index及其之后的元素进行复制,往后挪一位,最后将该index上元素的值更新


modCount的用处

用于集合修改时的记录,每次修改就+1

在Iterator的几个方法中

next() / remove() / previous() / set() / add()

modCount 会与 expectedModCount 相比较,不相等则抛出 异常ConcurrentModificationException() ,

目前为止一共看到两种情况会抛出该错误:

1.创立了Iterator对象之后使用其自带的add/remove/set以外的方法变动了数组结构,比如在迭代中使用ArrayList自己的remove方法。(ArrayList addAll?)如下:

在迭代过程中调用ArrayList自己的remove(int index)方法

public static void main(String args[]){ArrayList<Integer> al = new ArrayList<Integer>(6);al.add(0);al.add(1);al.add(2);Iterator<Integer> itr = al.iterator();int index = 0;while(itr.hasNext()){System.out.println(itr.next());al.remove(index);index++;}}

 

在Iterator创建之后使用了ArrayList自己的remove(index)方法
public static void main(String args[]){ArrayList<Integer> al = new ArrayList<Integer>(6);al.add(0);al.add(1);al.add(2);Iterator<Integer> itr = al.iterator();int index = 0;while(itr.hasNext()){System.out.println(itr.next());}al.remove(0);while(itr.hasNext()){System.out.println(itr.next());}}

2.非线程安全又使用迭代器的情况下多个线程改动了数组结构。

写了段代码演示了下,有点错误,会报java.lang.illegalStateException

import java.util.*;public class Test {public static void main(String args[]){ArrayList<Integer> al = new ArrayList<Integer>(50);for(int i=0;i<50;i++){al.add(i);}int index =0;Iterator<Integer> itr = al.iterator();Iterator<Integer> itr2 = al.iterator();Thread_Test t1 = new Thread_Test(itr);Thread_Test t2 = new Thread_Test(itr2);new Thread(t1).start();new Thread(t2).start();new Thread(t1).start();new Thread(t2).start();}}class Thread_Test  implements Runnable{private Iterator<Integer> itr;public Thread_Test(Iterator<Integer> i){itr = i;}public void run(){while(itr.hasNext()){System.out.println(itr.next());itr.remove();}}}class Thread_Test2  implements Runnable{private Iterator<Integer> itr;public Thread_Test2(Iterator<Integer> i){itr = i;}public void run(){while(true){if(itr.hasNext()){System.out.println(itr.next());itr.remove();}}}}

modCount的改动包括以下方法:

trimToSize() --容量没动也算ensureCaptacity()add(E e)add(int index, E element)remove(int i)fastRemove(int i)clear()addAll(Collection<? extends E> c)addAll(int index, Collection<? extends E> c)

removeRange(int fromIndex, int toIndex) 

Itr的remove()方法如下:

public void remove() {    if (lastRet == -1)throw new IllegalStateException();            checkForComodification();    try {AbstractList.this.remove(lastRet);if (lastRet < cursor)    cursor--;lastRet = -1;expectedModCount = modCount;    } catch (IndexOutOfBoundsException e) {throw new ConcurrentModificationException();    }}

可以看到这个方法是同步了expectedModCount 和 modCount的

类似的,继承了Itr的ListItr的set / add方法也同步了expectedModCount 和 modCount.

 


 

ArrayList的get(index)/set(index,element)/remove(index)方法内部都引用了RangeCheck(index)方法

   private void RangeCheck(int index) {if (index >= size)    throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);    }

可以看到这个方法检查方法传入的index是否超过了ArraList内部的size的值(size在每次数组的结构性变动时被更新),超过则抛出IndexOutOfBoundsException。

这里的结构性变动包括所有add/remove类型的操作,set类不算,列出如下:

add类操作:

add(E e)

add(int index, E element)

addAll(Collection<? extends E> c)

addAll(int index, Collection<? extends E> c)

remove类操作:

remove(int index) 

remove(Object o) 这个方法调用方法fastRemove(int index)的时候更新了size值

fastRemove(int index)

removeRange(int fromIndex, int toIndex)

因此如果在一个新生成的ArrayList里,添加第一个元素,然后set(1,5), 将会抛出 IndexOutOfBoundsException。

add&addAll&remove(object o)这些方法的返回类型boolean是做什么用的?为什么不用void?

搜到的说法是:查看方法执行后,集合的内容是否改变了:就是元素有无数量,位置等变化

那么 set(index,element)&remove(index) 这两个方法返回的返回的这个索引上的之前的值呢?又是做什么用的呢

原创粉丝点击