重复性:数据可以重复
null值:可以有null值存在
有序性:能保证数据插入有序
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
ArrayList继承AbstractList,父类中对部分接口进行实现
实现了List接口提供的方法,Serializable说明该类能够被序列化 ,能够被克隆、序列化
//有参构造,指定集合初始化大小
public ArrayList(int initialCapacity) {
super();
//指定大小不合法,则直接抛出异常
初始化数组大小
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
//无参构造
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)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
private static final int DEFAULT_CAPACITY = 10;
默认容量大小
private static final Object[] EMPTY_ELEMENTDATA = {};
默认数组大小
private transient Object[] elementData;
存储元素的数组
private int size;
集合存储元素的个数
首先需要进行扩容考虑,如果要扩容(size+1 > table.lengrh),按照1.5倍进行扩容
将元素插入数组尾部,并对计数size+1
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 > 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);
}
先找到元素存储位置(null和非null的对象判断相等的不同)
数组移动
注意:相同元素存在的情况下,只需找到从0号开始第一次出现的元素
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
}
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
E elementData(int index) {
return (E) elementData[index];
}
int size() 集合中存储元素的个数
boolean isEmpty() 判断当前集合是否为空,返回值为boolean类型 ----> false:
集合不为空 true:集合为空
boolean contains(object o) 判断当前集合是否包含Object对象
Iterator iterator() 迭代器,返回iterator实例
Object[] toArray() 将集合转化为数组
T[] toArray(T[] a) 将集合转化为数组
boolean add(E e) 添加元素
boolean remove(object o) 删除元素
boolean containsAll(Collection> c) 判断入参集合是否属于当前集合
boolean containsAll(Collection extend E> c) 对该集合添加新的子集合形成新的集合
boolean containsAll(int index, Collection extend E> c) 在指定位置添加新的子集合形成新的集合
void clear() 将集合消除掉
boolean equals(object o) 判断是否相等
E get(int index) 通过指定位置来获取元素
int indexOf(object o) 判断当前元素在集合中的位置,从前往后找
int lastIndexOf(object o) 判断当前元素在集合中的位置,从后往前找
list subList(int fromIndex,int toIndex) 找当前集合的子集,左开右闭,不包含右边的位置
class ArrayList2 <E> {
private E[] array;
private int usersize = 0;
public ArrayList2() {
this(10);
}
public ArrayList2(int size) {
this.array = (E[]) new Object[size];
}
//判满
public boolean isFull() {
if(this.usersize == array.length) {
return true;
}
return false;
}
//往集合里面添加元素 boolean add(E e)
public boolean add(E e) {
if(isFull()) {
this.array = Arrays.copyOf(array,array.length*2);
}
this.array[usersize++] = e;
return true;
}
//获取元素 E get(int index) 返回值:存储该位置元素
public E get(int i) {
if(i < 0 || i > array.length) {
throw new UnsupportedOperationException("i的位置异常");
}
else{
return array[i];
}
}
//删除元素 boolean remove(Object o)
public boolean remove(Object obj) {
for (int i = 0; i < usersize; i++) {
if(array[i] == obj) {
array[i] = array[i+1];
return true;
}
}
return false;
}
}
public class ArrayList1{
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
//往集合添加元素 boolean add(E e);
arrayList.add(12);
arrayList.add(34);
arrayList.add(12);
int size = arrayList.size();
System.out.println(size);
System.out.println(arrayList.isEmpty());
arrayList.add(null);
System.out.println(arrayList);
//获取元素 通过索引位置得到值 E get(int index) 返回值:存储该位置元素
Integer val1 = arrayList.get(1);
System.out.println(val1);
Integer val2 = arrayList.get(2);
System.out.println(val2);
//删除元素 boolean remove(int index) 按照下标删除
arrayList.remove(1);
System.out.println(arrayList);
System.out.println("========");
// boolean remove(Object o) 按照元素删除,但只删除相同元素的第一个
arrayList.remove(Integer.valueOf(12));
System.out.println(arrayList);
}
}