如果觉得我写的还行,请关注我的博客并且点个赞哟。本文主要介绍JAVA 中最常用的集合ArrayList相关知识。
1.ArrayList简介
1.1 .ArrayList简介
1.ArrayList是基于动态数组实现的,数组具有按索引查找的特性,所以访问很快,适合经常查询的数据。其实就是对数组的操作。
2.ArrayList 继承了AbstractList,实现了List。它是一个数组队列,提供了相关的添加、删除、修改、遍历等功能。
3.ArrayList 实现了RandmoAccess接口,即提供了随机访问功能。RandmoAccess是java中用来被List实现,为List提供快速访问功能的。在ArrayList中,我们即可以通过元素的序号快速获取元素对象;这就是快速随机访问。稍后,我们会比较List的“快速随机访问”和“通过Iterator迭代器访问”的效率。
4.ArrayList 实现了Cloneable接口,即覆盖了函数clone(),能被克隆。
5.ArrayList 实现java.io.Serializable接口,这意味着ArrayList支持序列化,能通过序列化去传输。
6.和Vector不同,ArrayList中的操作不是线程安全的!所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector或者CopyOnWriteArrayList。
1.2ArrayList关系图
1.3.ArrayList 构造方法
1. 默认构造函数
ArrayList()
2. capacity是ArrayList的默认容量大小。当由于增加数据导致容量不足时,容量会添加上一次容量大小的一半。
ArrayList(int capacity)
3. 创建一个包含collection的ArrayList
ArrayList(Collection<? extends E> collection)
1.4 ArrayList API
// Collection中定义的API
boolean add(E object)
boolean addAll(Collection<? extends E> collection)
void clear()
boolean contains(Object object)
boolean containsAll(Collection<?> collection)
boolean equals(Object object)
int hashCode()
boolean isEmpty()
Iterator<E> iterator()
boolean remove(Object object)
boolean removeAll(Collection<?> collection)
boolean retainAll(Collection<?> collection)
int size()
<T> T[] toArray(T[] array)
Object[] toArray()
// AbstractCollection中定义的API
void add(int location, E object)
boolean addAll(int location, Collection<? extends E> collection)
E get(int location)
int indexOf(Object object)
int lastIndexOf(Object object)
ListIterator<E> listIterator(int location)
ListIterator<E> listIterator()
E remove(int location)
E set(int location, E object)
List<E> subList(int start, int end)
// ArrayList新增的API
Object clone()
void ensureCapacity(int minimumCapacity)
void trimToSize()
void removeRange(int fromIndex, int toIndex)
1.5 ArrayList中的对象
//默认初始长度.
private static final int DEFAULT_CAPACITY = 10;
//Shared empty array instance used for empty instances.
private static final Object[] EMPTY_ELEMENTDATA = {};
//
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
//动态数组
transient Object[] elementData; // non-private to simplify nested class access
//数组长度
private int size;
ArrayList包含了两个重要的对象:elementData 和 size。
(01) elementData是"Object[]类型的数组",它保存了添加到ArrayList中的元素。实际上,elementData是个动态数组,我们能通过构造函数
ArrayList(int initialCapacity)来执行它的初始容量为initialCapacity;如果通过不含参数的构造函数ArrayList()来创建ArrayList,则elementData的容量默认是10。elementData数组的大小会根据ArrayList容量的增长而动态的增长,具体的增长方式,请参考源码分析中的ensureCapacity()函数。(02) size 则是动态数组的实际大小。
1.6 ArrayList 扩容
当ArrayList容量不足以容纳全部元素时,ArrayList会重新设置容量:新的容量=“(原始容量x3)/2 + 1”。具体代码如下
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);
// 扩容
elementData = Arrays.copyOf(elementData, newCapacity);
}
1.7 ArrayList 的遍历
ArrayList支持的3种遍历方式
(01) 第一种,通过迭代器遍历。即通过Iterator去遍历。
Integer value = null;
Iterator iter = list.iterator();
while (iter.hasNext()) {
value = (Integer)iter.next();
}
(02) 第二种,随机访问,通过索引值去遍历。 由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素。
Integer value = null;
int size = list.size();
for (int i=0; i<size; i++) {
value = (Integer)list.get(i);
}
(03) 第三种,for循环遍历。如下:
Integer value = null;
for (Integer integ:list) {
value = integ;
}
如果你觉得我写的还行,请关注我的博客并且点个赞哟,也请关注我的公众号并加入下方QQ群,每天都会定时推送干货以及共享一些优质的学习资料和视频哟.