ArrayList 类
1、ArrayList概述
1.1 实现的接口
Collection接口 :里面相当于描述List的ADT(抽象数据类型)
常见方法如下:
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
void clear();
boolean contains(Object o);
boolean add(E e);
boolean remove(Object o);
Iterator<E> iterator();
.......
}
List接口:List接口继承了Collection接口,相当于对以上操作的一个补充
额外添加的方法:
public interface List<E> extends Collection<E> {
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
.......
}
1.2 ArrayList类扩容机制
1)、默认大小
private static final int DEFAULT_CAPACITY = 10;
可以看出,默认大小为10
2)、数组容量不够时的扩容方法:
① 添加元素时的add方法
public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
ensureCapacityInternal方法会检测数组的容量,若容量不够按照相应的规则进行扩展。
② 流程方法
private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
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);
}
2、简易ArrayList类的实现
package cn.codingboy.datastructure.List;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class MyArrayList<T> {
private static final int DEFAULT_CAPACITY = 10;
private int size;
private T[] Items;
public MyArrayList() {
doClear();
}
public void clear() {
doClear();
}
private void doClear() {
size = 0;
ensureCapacity(DEFAULT_CAPACITY);
}
public int size() {
return size;
}
public boolean isEmpty() {
return size() == 0;
}
public void trimToSize() {
ensureCapacity(size());
}
public T get(int index) {
if (index < 0 || index >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
return Items[index];
}
public T set(int index, T newVal) {
if (index < 0 || index >= size()) {
throw new ArrayIndexOutOfBoundsException();
}
T old = Items[index];
Items[index] = newVal;
return old;
}
private void ensureCapacity(int newCapacity) {
if (newCapacity < size) {
return;
}
T[] old = Items;
Items = (T[]) new Object[newCapacity];
for (int i = 0; i < size(); i++) {
Items[i] = old[i];
}
}
public boolean add(T x) {
add(size(), x);
return true;
}
private void add(int index, T x) {
if (Items.length == size()) {
ensureCapacity(size() * 2 + 1);
}
for (int i = size; i > index; i--) {
Items[i] = Items[i - 1];
}
Items[index] = x;
size++;
}
public T remove(int index) {
T rem = Items[index];
for (int i = index; i < size() - 1; i++) {
Items[i] = Items[i + 1];
}
size--;
return rem;
}
public Iterator<T> iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements Iterator<T> {
private int current = 0;
@Override
public boolean hasNext() {
return current < size();
}
@Override
public T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return Items[current++];
}
@Override
public void remove() {
MyArrayList.this.remove(--current);
}
}
public static void main(String[] args) {
MyArrayList list = new MyArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
|