- 测试代码
@Test
public void testIterator(){
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.addElem(1);
arrayList.addElem(2);
arrayList.addElem(3);
for (Integer i : arrayList) {
System.out.println(i);
}
}
- ArrayList代码
public class ArrayList<E> implements List<E>, Iterable<E> {
//默认存储容器大小
private int defaultCapacity = 10;
//存储容器
private Object[] elemData;
//元素个数
private int size;
/**
* 初始化存储容器
*/
public ArrayList() {
elemData = new Object[defaultCapacity];
}
/**
* 初始化存储容器并且添加元素
*
* @param initialList 待添加的线性表
*/
public ArrayList(List<? extends E> initialList) {
this();
addAll(initialList);
}
/**
* 初始化存储容器
*
* @param initialCapacity 存储容器初始大小
*/
public ArrayList(int initialCapacity) {
elemData = new Object[initialCapacity];
}
/**
* 检查位置是否合法
*
* @param index 待检查位置
* @throws IndexOutOfBoundsException 位置不合法异常
*/
private void rangeCheck(int index) throws IndexOutOfBoundsException {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(index + "超出线性表范围");
}
}
/**
* 确保容量够用
*
* @param length 添加后的容量
*/
private void ensureCapacity(int length) {
if (length > elemData.length) {
extendedCapacity(length);
}
}
/**
* 1.5倍扩容
*
* @param length 至少需要的大小
* @throws OutOfMemoryError 分配内存失败
*/
private void extendedCapacity(int length) throws OutOfMemoryError {
int extendedLength = length;
extendedLength = extendedLength + extendedLength >> 1;
try {
elemData = Arrays.copyOf(elemData, extendedLength);
} catch (OutOfMemoryError error) {
throw new OutOfMemoryError("扩容失败");
}
}
public void clean() {
elemData = new Object[defaultCapacity];
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int length() {
return size;
}
public void addElem(E elem) {
ensureCapacity(size + 1);
elemData[size++] = elem;
}
public void addAll(List<? extends E> list) {
if (list == null) {
return;
}
ensureCapacity(list.length() + size);
System.arraycopy(list.toArray(), 0, elemData, size, list.length());
size += list.length();
}
public void insertElem(int index, E elem) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(index + "超出线性表范围");
}
ensureCapacity(size + 1);
System.arraycopy(elemData, index, elemData, index + 1, size++ - index);
elemData[index] = elem;
}
public int replace(E target, E replace) {
int count = 0;
if (target == null) {
for (int i = 0; i < size; i++) {
if (elemData[i] == null) {
count++;
elemData[i] = replace;
}
}
} else {
for (int i = 0; i < size; i++) {
if (elemData[i].equals(target)) {
count++;
elemData[i] = replace;
}
}
}
return count;
}
public E indexOf(int index) {
rangeCheck(index);
return (E) elemData[index];
}
public int locateElem(E elem) {
if (elem == null) {
for (int i = 0; i < size; i++) {
if (elemData[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < size; i++) {
if (elemData[i].equals(elem)) {
return i;
}
}
}
return -1;
}
public List<Integer> locateElements(E elem) {
List<Integer> result = new ArrayList<>();
if (elem == null) {
for (int i = 0; i < size; i++) {
if (elemData[i] == null) {
result.addElem(i);
}
}
} else {
for (int i = 0; i < size; i++) {
if (elemData[i] != null && elemData[i].equals(elem)) {
result.addElem(i);
}
}
}
return result;
}
public int removeElements(E elem) {
int result = 0;
List<Integer> list = locateElements(elem);
for (int i = 0; i < list.length(); i++) {
removeElem(list.indexOf(i) - result++);
}
return result;
}
/**
* 删除元素
* @param index 删除的位置
* @return 删除的元素
*/
public E removeElem(int index) {
E result = (E) elemData[index];
System.arraycopy(elemData, index + 1, elemData, index, size-- - index - 1);
return result;
}
public Object[] toArray() {
return Arrays.copyOf(elemData, size);
}
@Override
public Iterator<E> iterator() {
return new Left2Right<E>();
}
private class Left2Right<Q> implements Iterator<Q>{
private int index = 0;
@Override
public boolean hasNext() {
if (index < size) {
return true;
}
return false;
}
@Override
public Q next() {
return (Q) elemData[index++];
}
}
}
网友评论