定义:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
}
变量:
private static final int DEFAULT_CAPACITY = 10; 默认的初始容量
private static final Object[] EMPTY_ELEMENTDATA = {}; 空的数据容器
private transient Object[] elementData; 数据容器
private int size; 数据量
protected transient int modCount = 0 ; 数据容器update的次数
构造方法:
① public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA; 数据容器为空的数据容器
}
② public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652) </span>
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
E 为数据的泛型, 将c中所有c.size的数据存放在容器中,size = c.size, 6260652是JDKbug库里面的一个bug编号,害怕上述操作无法转型成Object[] ,后面又copy了一次
方法:
public void trimToSize() {
modCount++;
if (size < elementData.length) {
elementData = Arrays.copyOf(elementData, size);
}
}把elementData 的length 缩小为size
public void ensureCapacity(int minCapacity) {
int minExpand = (elementData != EMPTY_ELEMENTDATA)
? 0
: DEFAULT_CAPACITY;
if (minCapacity > minExpand) {
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:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
是否扩容:① 如果minCapacity < minExpand 不扩容
②minCapacity >minExpand && minCapacity< =elementData.length modCount++ ,不扩容
③minCapacity >minExpand && minCapacity >elementData.length modCount++,elementData 扩容
扩容的情况: 首先是原长度的2/3(奇数就是2/3上-1)是否>minCapacity ,minCapacity取较小值 ,如果minCapacity - (Integer.MAX_VALUE- 8)>0 那么就调用hugeCapacity;如果 minCapacity超出了int的最大值会报内存溢出
<span style="font-family: Arial, Helvetica, sans-serif;">当minCapacity较大且还不大于Integer.MAX_VALUE 我这边要报Requested array size exceeds VM limit,超出了我堆内存的设置大小,一般不会new 太大的数据,如果太大还是考虑一下程序逻辑吧</span>
public int size() {
return size;
}
获取集合里面实际的数据量<pre name="code" class="java">public boolean isEmpty() {
return size == 0;
}
是否没有实际的数据
<pre name="code" class="java"> public boolean contains(Object o) {
return indexOf(o) >= 0;
}
是否有元素o
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第一次出现的位置,找不到返回-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;
}
反序遍历实际的数据量 找到o第一次出现的位置,找不到返回-1
public Object clone() {
try {
@SuppressWarnings("unchecked")
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
实现了Cloneable接口,重写了Object的conle(),length缩减为size,modCount =0 ,其他参数和原来一样.
关于clone详见:http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html