一、List接口
public interface List<E> {
//获取数据大小
int getSize();
//添加数据
void add(E e);
//指定下标添加数据
void addIndex(int index, E e);
//移除指定下标数据
E remove(int index);
//获取指定下标数据
E getIndex(int index);
//更新指定下标数据
void update(int index, E e);
//是否为空
boolean isEmpty();
}
二、MyArrayList实现
public class MyArrayList<E> implements List<E> {
private E[] data;
private int size;
public MyArrayList(int capacity) {
data = (E[]) new Object[capacity];
size = 0;
}
public MyArrayList() {
this(10);
}
@Override
public int getSize() {
return size;
}
@Override
public void add(E e) {
addLast(e);
}
@Override
public boolean isEmpty() {
return size == 0;
}
public void addLast(E e) {
addIndex(size - 1, e);
}
public E removeFirst() {
return remove(0);
}
public E removeLast() {
return remove(size - 1);
}
@Override
public void addIndex(int index, E e) {
if (size == data.length) {
throw new IllegalArgumentException("Add failed,Array is fulled");
}
if (index < 0 || index > size - 1) {
throw new IllegalArgumentException("index is invalidate");
}
//动态扩容
if (size == data.length) {
resize(data.length * 2);
}
//数据搬移
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
}
@Override
public E getIndex(int index) {
if (index < 0 || index > size - 1) {
throw new IllegalArgumentException("Index is invalidate");
}
return data[index];
}
public E getLast() {
return getIndex(size - 1);
}
public E getFirst() {
return getIndex(0);
}
@Override
public E remove(int index) {
if (index < 0 || index > size - 1) {
throw new IllegalArgumentException("Index is invalidate");
}
E ret = data[index];
//数据搬移
for (int i = index; i < data.length; i++) {
data[i - 1] = data[i];
}
size--;
data[size] = null;
//动态缩容
if (size == data.length / 2) {
resize(data.length / 2);
}
return ret;
}
@Override
public void update(int index, E e) {
data[index] = e;
}
private void resize(int newCapacity) {
E[] newData = (E[]) new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
}
三、JDK源码中的ArrayList
3.1、List接口
public interface List<E> extends Collection<E> {
//数据大小
int size();
//是否为空
boolean isEmpty();
//是否包含某个数据
boolean contains(Object o);
//迭代器
Iterator<E> iterator();
//转化为数组
Object[] toArray();
<T> T[] toArray(T[] a);
//添加数据
boolean add(E e);
//移除数据
boolean remove(Object o);
//是否包含入参的所有数据
boolean containsAll(Collection<?> c);
//添加入参的所有数据
boolean addAll(Collection<? extends E> c);
//从index位置添加入参的所有数据
boolean addAll(int index, Collection<? extends E> c);
//移除所有数据
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
default void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final ListIterator<E> li = this.listIterator();
while (li.hasNext()) {
li.set(operator.apply(li.next()));
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
default void sort(Comparator<? super E> c) {
Object[] a = this.toArray();
Arrays.sort(a, (Comparator) c);
ListIterator<E> i = this.listIterator();
for (Object e : a) {
i.next();
i.set((E) e);
}
}
//清空数据
void clear();
//是否相等
boolean equals(Object o);
//该列表的hashCode
int hashCode();
//获取对应index的数据
E get(int index);
//设置index数据为element
E set(int index, E element);
//添加element数据到index下标
void add(int index, E element);
//移除下标为index的数据
E remove(int index);
//获取该对象的下标
int indexOf(Object o);
//获取为该数据的最后下标
int lastIndexOf(Object o);
//返回该列表数据的迭代器
ListIterator<E> listIterator();
//返回从index开始的列表数据的迭代器
ListIterator<E> listIterator(int index);
//返回从fromIndex到toIndex间的数据
List<E> subList(int fromIndex, int toIndex);
@Override
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, Spliterator.ORDERED);
}
}
3.2、ArrayList JDK实现
3.2.1、默认容量为10
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
3.2.2、动态数据的实现
- 新容量的计算:旧的容量+旧的容量右移1位的值
int newCapacity = oldCapacity + (oldCapacity >> 1);
- 数据的搬移:Arrays.copyOf() ---->System.arraycopy()
android-10.0.0_r41\libcore\ojluni\annotations\hiddenapi\java\util\Arrays.java
public static byte[] copyOf(byte[] original, int newLength) {
byte[] copy = new byte[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
android-10.0.0_r41\libcore\ojluni\src\main\java\java\lang\System.java
private static void arraycopy(int[] src, int srcPos, int[] dst, int dstPos, int length) {
if (src == null) {
throw new NullPointerException("src == null");
}
if (dst == null) {
throw new NullPointerException("dst == null");
}
if (srcPos < 0 || dstPos < 0 || length < 0 ||
srcPos > src.length - length || dstPos > dst.length - length) {
throw new ArrayIndexOutOfBoundsException(
"src.length=" + src.length + " srcPos=" + srcPos +
" dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length);
}
if (length <= ARRAYCOPY_SHORT_INT_ARRAY_THRESHOLD) {
// Copy int by int for shorter arrays.
if (src == dst && srcPos < dstPos && dstPos < srcPos + length) {
// Copy backward (to avoid overwriting elements before
// they are copied in case of an overlap on the same
// array.)
for (int i = length - 1; i >= 0; --i) {
dst[dstPos + i] = src[srcPos + i];
}
} else {
// Copy forward.
for (int i = 0; i < length; ++i) {
dst[dstPos + i] = src[srcPos + i];
}
}
} else {
// Call the native version for longer arrays.
arraycopyIntUnchecked(src, srcPos, dst, dstPos, length);
}
}
四、时间复杂度分析
操作 | 时间复杂度 |
---|---|
增加 | O(n) |
删除 | O(n) |
修改 | O(1) |
查询 | O(1) |