注意:此处只是实现了ArrayList部分方法
1.首先新建一个List接口,内容如下:
import java.util.ArrayList;
import java.util.Collection;
import com.jinzhi.Student;
public interface List<E> {
// 求长度
int size();
// 集合内是否有元素
boolean isEmpty();
// 某个元素是否在集合内
boolean contains(Object o);
// 把集合内元素转换为数组
Object[] toArray();
// 向集合内添加元素
boolean add(E e);
// 删除集合内的某个元素
boolean remove(Object o);
// 删除某个位置的元素
E remove(int index);
// 清空集合内元素
void clear();
// 根据下标得到集合内某个元素
E get(int index);
// 在某个位置添加元素
void add(int index, E element);
//从index位置开始插入此集合c
boolean addAll(int index, Collection<? extends E> c);
//删除两个集合重复的元素
boolean removeAll(Collection<?> c);
}
2.接着是它的实现类ArrayList:
import java.util.Arrays;
import java.util.Collection;
public class ArrayList<E> implements List<E>{
private Object[] elementDate = new Object[0];
private int size = 0;
public ArrayList() {
}
public ArrayList(int length) {
this.elementDate = new Object[length];
}
@Override
public boolean add(E e) {
try {
if (elementDate.length == 0) {
elementDate = new Object[2];
}
//如果数组中数据已满
if (size == elementDate.length) {
int oldLength = elementDate.length;
//长度变为已有长度的1.5倍
int newLength = oldLength + (oldLength >> 1);
//创建新的数组并且把已有数据放入新数组
elementDate = Arrays.copyOf(elementDate, newLength);
}
//把新元素放入数组,且长度+1
elementDate[size++] = e;
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return false;
}
return false;
}
@Override
public int size() {
return this.size;
}
@Override
public boolean isEmpty() {
return (this.size == 0);
}
@Override
public boolean contains(Object o) {
for (int i = 0; i < size; i++) {
if (elementDate[i] == o) {
return true;
}
}
return false;
}
@Override
public Object[] toArray() {
Object[] data = Arrays.copyOf(elementDate, size);
return data;
}
@Override
public boolean remove(Object o) {
int index = -1;
for (int i = 0; i < size; i++) {
if (o == elementDate[i]) {
index =i;
break;
}
}
if (index == -1) {
return false;
} else {
this.remove( index);
return true;
}
}
@Override
public E remove(int index) {
if (index > size) {
throw new ArrayIndexOutOfBoundsException();
}
Object deleteO = elementDate[index];
for (int i = index + 1; i < size; i++) {
elementDate[i -1] = elementDate[i];
}
size--;
elementDate[size] = null;
return (E) deleteO;
}
@Override
public void clear() {
size = 0;
this.elementDate = new Object[0];
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException("下标超出范围");
}
return (E) this.elementDate[index];
}
@Override
public void add(int index, E element) {
if (index < 0 || index >= size) {
throw new ArrayIndexOutOfBoundsException("下标超出范围");
}
if (size == elementDate.length) {
int oldLength = elementDate.length;
//长度变为已有长度的1.5倍
int newLength = oldLength + (oldLength >> 1);
//创建新的数组并且把已有数据放入新数组
elementDate = Arrays.copyOf(elementDate, newLength);
}
//移动老数据
for (int i = size; i > index; i--) {
elementDate[i] = elementDate[i-1];
}
elementDate[index] = element;
size ++;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
}