成员变量:这个类里面有两个私有成员变量:
private transient Object[] elementData;//ArrayList底层维护的是一个Object类型的数组,这样就是使得该集合可以存放任何类型的数据,而这些元素就存放着这个数组中
private int size;//该变量用于记录该集合中所包含元素的个数,而不是底层数组的长度,每增加或删除一个元素,size的值就会相应地增加或减小
构造方法:这个类有三个构造方法:
public ArrayList(int initialCapacity)//构建一个带有初始容量的ArrayList集合,该方法会先判断给定数值是否<0,然后才会创建数组,这种每次操作集合之前先判断的做法在Java源码中普遍存在。源码如下:
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
public ArrayList()
public ArrayList() {
this(10);
}
public ArrayList(Collection<? extends E> c)//以一个集合为参数创建一个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);
}
实用方法:
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
作用:整理集合的容量,使得集合的容量值与当前集合中所包含的元素数相等,即capacity = size。这样做可以节约内存空间。
modCount变量是该类的父类AbstractList<E>中的成员变量,用于记录对集合接口进行操作的次数。
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;
}
作用:返回指定元素在该集合中首次出现的位置,如果不存在则返回-1。这个方法是contains(Object o)方法的基础,不再赘述。
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;
}
作用:返回指定元素在该集合中最后一次出现的位置,如果不存在则返回-1。效果跟indexOf(Object o)方法正好相反。
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
作用:以合适的序列(一般是从第一个元素到最后一个元素)返回该集合对应的Object类型数组。
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
作用:以合适的序列(一般是从第一个元素到最后一个元素)返回该集合对应的指定类型数组。相当于返回该集合包含的元素所对应的类型数组(转型后的数组)。
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
作用:返回指定数组下标上的元素,操作之前先判断下标是否越界。
public E set(int index, E element) {
RangeCheck(index);
E oldValue = (E) elementData[index];
elementData[index] = element;
return oldValue;
}
作用:用指定元素替换指定下标位置上的元素,并返回被替换的元素。操作之前先判断下标是否越界。
public boolean add(E e) {
ensureCapacity(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
作用:在集合的最后添加一个元素,操作之前先确保数组的容量,若容量到达预警值则对数组进行扩容。
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
ensureCapacity(size+1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
作用:将指定元素插入到数组的指定位置。先对下标进行判断,然后确保数组的容量,最后进行插入操作。注意:该操作会对数组进行复制,首相将原数组指定位置及以后的元素依次往后挪一个位置,创建空出指定位置的新数组,然后将指定元素插入到该位置。该操作的成本比较高。
public E remove(int index) {
RangeCheck(index);
modCount++;
E oldValue = (E) 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;
}
作用:删除指定位置上的元素,并返回该元素。跟上面的方法一样,这个方法也会修改创建一个将指定位置删除的新数组。该操作的成本比较高。
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;
}
作用:删除指定的元素。注:该方法只会删除数组中第一次出现指定元素位置上的元素。跟上面的方法一样,这个方法也会修改创建一个将指定位置删除的新数组。该操作的成本比较高。
public void clear() {
modCount++;
// Let gc do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
作用:清空该集合。
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
作用:将一个集合中的所有元素添加到该集合的尾部。
public boolean addAll(int index, Collection<? extends E> c) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
作用:将一个集合中的所有元素添加到指定该集合的指定位置,原集合指定位置及其以后的元素依次往后挪一个位置。该操作的成本比较高。
2016年9月7日 18:06