import java.util.Arrays;
public class MyArrayList<E> {
private Object[] elements;
private static final int DEFAULT_CAPACITY = 10;
private static final int MAX_ARRAY_SIZE = 2147483639;
private int size = 0;
private boolean flag;
public MyArrayList() {
this.elements = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int initCapacity) {
if (initCapacity >= 0 && initCapacity <= MAX_ARRAY_SIZE){
this.elements = new Object[initCapacity];
} else {
throw new IllegalArgumentException
("IllegalArgumentException : " + initCapacity);
}
}
public boolean add(E e) {
return this.add(this.size, e);
}
public boolean add(int index, E e) {
if (index >= 0 && index <= this.size) {
this.ensureCapacity(this.size + 1);
for(int i = this.size; i > index; --i) {
this.elements[i] = this.elements[i - 1];
}
this.elements[index] = e;
++this.size;
return true;
} else {
throw new ArrayIndexOutOfBoundsException(index);
}
}
public boolean addAll(MyArrayList<? extends E> list) {
Object[] array = list.toArray();
int newSize = array.length;
this.ensureCapacity(this.size + newSize);
for(int i = 0; i < newSize; ++i) {
this.elements[i + this.size] = array[i];
}
this.size += newSize;
return true;
}
public boolean addAll(int index, MyArrayList<? extends E> list) {
return true;
}
public boolean remove(Object obj) {
int index = this.indexOf(obj);
return this.remove(index) != null;
}
public E remove(int index) {
if (-1 == index) {
return null;
} else {
E e = this.get(index);
for(int i = index; i < this.size - 1; ++i) {
this.elements[i] = this.elements[i + 1];
}
this.elements[this.size - 1] = null;
--this.size;
return e;
}
}
還有其他的用法,等待下次更新