用途和解释
Java.util.ArrayList类是一个动态数组队列,可以随时从链表中添加或删除一个元素。ArrayList实现了List接口并且可以动态改变大小的。当我们不知道数据元素的个数时,就可使用ArrayList;与Java中的数组相比,它的容量能动态增长。它继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。和Vector不同,ArrayList中的操作不是线程安全的。所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector
实例
List<String> lists = new ArrayList<String>();(其中的String可以改为integer,double等类型)
注:ArrayList对象只能存放对象,不能存放基础数据类型的数据。
源码分析结构
源码继承结构
ArrayList 继承了 AbstractList 抽象类,实现了 List 接口、RandomAccess 接口、Cloneable 接口、java.io.Serializable 接口
List 接口
有序的集合,存储元素和取出元素的顺序是一致的
允许存储重复的元素(包括空)
上诉可以看到,List 将 Collection 中的抽象方法重新定义了一遍
void add(int index, E element); // 向指定索引处增加元素 0 <= index <= size
boolean addAll(int index, Collection<? extends E> c); // 向指定索引处增加集合中的所有元素 0 <= index <= size
E remove(int index); // 删除指定索引位置的元素 0 <= index < size
E set(int index, E element); // 修改指定索引处的元素 0 <= index < size
E get(int index); // 获取指定索引处的元素 0 <= index < size
int indexOf(Object o); // 获取指定元素第一次出现的索引,若不存在则返回-1
int lastIndexOf(Object o); // 获取指定元素最后一次出现的索引,若不存在则返回-1
List<E> subList(int fromIndex, int toIndex); // 返回集合中从fromIndex(含)到toIndex(不含)的视图,与原集合会相互影响
ListIterator<E> listIterator(); // 返回listIterator hasPrevious()、previous()、add(E e);
ListIterator<E> listIterator(int index); // 从指定索引处开始返回listIterator 0 <= index <= size
// jdk1.8新增方法
default void sort(Comparator<? super E> c){
} // 根据Comparator来排序数组
default void replaceAll(UnaryOperator<E> operator) {
} // 将该列表的每个元素替换为该运算符应用于该元素的结果
AbstractList 抽象类
在 AbstractList 抽象父类中:有些方法已经被赋予了具体的实现逻辑,比如 indexOf() 和 lastIndexOf() 方法;而有些方法则需要推迟到子类中去实现,比如 get()、set()、add()、remove() 等方法
// 维护了集合被修改的次数
protected transient int modCount = 0;
//在集合末尾添加元素,由子类实现
public boolean add(E e) {
add(size(), e);
return true;
}
/**
* {@inheritDoc}
*
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
abstract public E get(int index);
// 限定了add、set、remove操作, 由子类实现
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public E remove(int index) {
throw new UnsupportedOperationException();
}
// Search Operations
/**
* {@inheritDoc}
*
* <p>This implementation first gets a list iterator (with
* {@code listIterator()}). Then, it iterates over the list until the
* specified element is found or the end of the list is reached.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
// 从前往后找,获取指定元素在集合中第一次出现的索引,若不存在则返回-1
public int indexOf(Object o) {
ListIterator<E> it = listIterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return it.previousIndex();
} else {
while (it.hasNext())
if (o.equals(it.next()))
return it.previousIndex();
}
return -1;
}
/**
* {@inheritDoc}
*
* <p>This implementation first gets a list iterator that points to the end
* of the list (with {@code listIterator(size())}). Then, it iterates
* backwards over the list until the specified element is found, or the
* beginning of the list is reached.
*
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
// 从后往前找,获取指定元素在集合中第一次出现的索引,若不存在则返回-1
public int lastIndexOf(Object o) {
ListIterator<E> it = listIterator(size());
if (o==null) {
while (it.hasPrevious())
if (it.previous()==null)
return it.nextIndex();
} else {
while (it.hasPrevious())
if (o.equals(it.previous()))
return it.nextIndex();
}
return -1;
}
// Bulk Operations
/**
* Removes all of the elements from this list (optional operation).
* The list will be empty after this call returns.
*
* <p>This implementation calls {@code removeRange(0, size())}.
*
* <p>Note that this implementation throws an
* {@code UnsupportedOperationException} unless {@code remove(int
* index)} or {@code removeRange(int fromIndex, int toIndex)} is
* overridden.
*
* @throws UnsupportedOperationException if the {@code clear} operation
* is not supported by this list
*/
public void clear() {
removeRange(0, size());
}
/**
* {@inheritDoc}
*
* <p>This implementation gets an iterator over the specified collection
* and iterates over it, inserting the elements obtained from the
* iterator into this list at the appropriate position, one at a time,
* using {@code add(int, E)}.
* Many implementations will override this method for efficiency.
*
* <p>Note that this implementation throws an
* {@code UnsupportedOperationException} unless
* {@link #add(int, Object) add(int, E)} is overridden.
*
* @throws UnsupportedOperationException {@inheritDoc}
* @throws ClassCastException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc}
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
// 向指定索引处增加集合中的所有元素, add由子类实现
public boolean addAll(int index, Collection<? extends E> c) {
//索引检查 index < 0 || index > size()
rangeCheckForAdd(index)