版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
ArraysList实现类
继承关系:
属于collection的子接口list的一个实现类
特点:
1、可存储重复性数据
2、可以存储null值
3、插入有序
常用方法介绍:
//添加元素
boolean add(E e) 添加元素 ,添加指定的类型
//统计集合元素个数
int size() 返回集合中元素的个数
//获取元素
E get(int index)
//删除元素
remove() 删除元素(两种删除方式:按下标删除、按对象删除)
//修改操作
/**
* E set(int index, E element)
* 参数1 index:表示修改元素的下标
* 参数2 element:新数据
*
* 返回结果: 下标为index源存储值
*/
E set(int index, E element)
//removeAll()删除元素 删除指定的集合元素
arrayList.removeAll(arrayList2);
//contains()集合是是否包含元素 在当前集合中是否包含指定的元素 ,存在则返回true,不存在返回 false
System.out.println(arrayList.contains(1));
//retainAll(a) 两集合的交集
b.retainAll(a);
//toArray() 将集合转化为数组
Integer[] array = (Integer[]) a.toArray();
ArrayList源码实现
源码关注点:
1、继承关系
2、存在的属性和默认的属性值
3、常用方法的源码查看
集合的源码:JDK 1.7说明
继承关系:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
通过源码分析:ArrayList继承自AbstractList父类
实现的接口有:List ,Cloneable, java.io.Serializable
说明ArrayList实现类支持克隆,支持序列化
属性介绍:
默认的数组大小:10
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
ArrayList的底层存储使用的数组
private transient Object[] elementData;
//表示集合中存储的元素的个数
private int size;
知道:ArrayList底层存储的数据结构:数组
默认的数组大小是10
构造函数:
有参构造函数:传递的参数:表示初始化数组大小
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
//直接实例化数组的大小为:initialCapacity
this.elementData = new Object[initialCapacity];
}
//无参构造
public ArrayList() {
super();
this.elementData = {} EMPTY_ELEMENTDATA;
}
//有参构造
//参数为Collection接口的实例对象 -》hashmap
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);
}
常用的方法:
add():添加元素
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity > elementData.length)
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);
}
add过程:先判断ArrayList容量是否足够,不满足则扩容:
扩容是按照原来的数组长度(element.length)的1.5倍扩容
进行数据添加
get():获取元素
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
//参数index的范围检查
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
get操作过程:首先检查index的合法性问题,直接通过数据elementData下标访问获取数据
remove():删除元素
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;
}
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
删除过程:首先判断当前对象是否是null值对象
null对象通过==来判断集合中是否存在
非null值对象通过equals来判断集合中是否存在
进行数据移动:将删除对象位置之后的数据前移一位
ArrayList中重复性存储的数据,找到第一个元素直接删除,后续的重复性元素不在处理
set():修改元素
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}