MyList<E>接口:
package org.mobiletrain.util;
public interface MyList<E> {
/**
* 向容器中添加一个元素
* @param element
*/
public void add(E element);
/**
* 向容器中添加一组元素
* @param arrayOfElements 元素的数组
*/
public void add(E[] arrayOfElements);
/**
* 删除指定的元素(首次出现的位置)
* @param e 待删除的元素
* @return 如果元素存在返回true否则返回false
*/
public boolean remove(E e);
/**
* 删除指定的元素
* @param e 元素
* @param allOccurence 如果为true则删除所有位置上的该元素否则只删除首次出现的位置
* @return 如果元素存在返回true否则返回false
*/
public boolean remove(E e, boolean allOccurence);
/**
* 删除指定位置的元素
* @param index 元素的位置(索引)
* @return 被删除的元素
*/
public E removeAtIndex(int index);
// public E[] removeOfRange(int start, int end);
/**
* 修改指定位置的元素
* @param index 元素的位置(索引)
* @param element 新元素
* @return 被修改的旧元素
*/
public E set(int index, E element);
// public boolean contains(E element);
/**
* 查找元素在容器中首次出现的位置
* @param element 元素
* @return 找到了返回元素首次出现的位置(索引)否则返回-1
*/
public int indexOf(E element);
/**
* 获取指定位置的元素
* @param index 元素的位置(索引)
* @return 元素
*/
public E get(int index);
/**
* 用指定的位置获取当前容器的子容器
* @param fromIndex 起始位置(包含)
* @param toIndex 终止位置(不包含)
* @return 子容器
*/
public MyList<E> subList(int fromIndex, int toIndex);
/**
* 是不是空容器
* @return 容器没有元素返回true否则返回false
*/
public boolean isEmpty();
/**
* 清空容器
*/
public void clear();
/**
* 容器的大小
* @return 容器中元素的个数
*/
public int size();
}
MyArrayList<E>去实现MyList<E>:
package org.mobiletrain.util;
import java.util.Arrays;
public class MyArrayList<E> implements MyList<E> {
private static final int DEFAULT_INIT_SIZE = 1 << 4;
private static final Object[] DEFAULT_EMPTY_ARRAY = {};
private E[] elements = null;
private int size = 0;
public MyArrayList() {
this(DEFAULT_INIT_SIZE);
}
public MyArrayList(int capacity) {
if (capacity > 0) {
elements = (E[]) new Object[capacity];
}
else if (capacity == 0) {
elements = (E[]) DEFAULT_EMPTY_ARRAY;
}
else {
throw new IllegalArgumentException("列表容器的容量不能负数");
}
}
public MyArrayList(E[] array) {
assert array != null;
elements = (E[]) new Object[array.length];
for (int i = 0; i < array.length; ++i) {
if (array[i] != null) {
elements[size++] = array[i];
}
}
}
@Override
public void add(E e) {
if (e != null) {
ensureCapacity(size + 1);
elements[size++] = e;
}
}
@Override
public void add(E[] arrayOfElements) {
assert arrayOfElements != null;
ensureCapacity(size + arrayOfElements.length);
for (int i = 0; i < arrayOfElements.length; ++i) {
if (arrayOfElements[i] != null) {
elements[size++] = arrayOfElements[i];
}
}
}
@Override
public boolean remove(E e) {
assert e != null;
int index = indexOf(e);
if (index != -1) {
removeAtIndex(index);
return true;
}
return false;
}
@Override
public boolean remove(E e, boolean allOccurence) {
int counter = 0;
int index;
while ((index = indexOf(e)) != -1) {
removeAtIndex(index);
counter += 1;
}
return counter > 0;
}
// 1 3 4 5 null
@Override
public E removeAtIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("访问元素索引越界: " + index);
}
E e = elements[index];
for (int i = index + 1; i < size; ++i) {
elements[i - 1] = elements[i];
}
elements[--size] = null;
return e;
}
@Override
public E set(int index, E e) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("");
}
E old = elements[index];
elements[index] = e;
return old;
}
@Override
public int indexOf(E e) {
assert e != null;
for (int i = 0; i < size; ++i) {
if (e.equals(elements[i])) {
return i;
}
}
return -1;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("");
}
return elements[index];
}
@Override
public MyList<E> subList(int fromIndex, int toIndex) {
assert toIndex > fromIndex;
if (fromIndex < 0 || toIndex > size) {
throw new IndexOutOfBoundsException("");
}
E[] newArray = Arrays.copyOfRange(elements, fromIndex, toIndex);
return new MyArrayList<E>(newArray);
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public void clear() {
for (int i = 0; i < size; ++i) {
elements[i] = null;
}
size = 0;
}
@Override
public int size() {
return size;
}
private void ensureCapacity(int minCapactiy) {
if (elements.length < minCapactiy) {
int oldCapacity = elements.length;
int newCapacity = oldCapacity + oldCapacity >> 1;
newCapacity = newCapacity < minCapactiy ? minCapactiy : newCapacity;
elements = Arrays.copyOf(elements, newCapacity);
// E[] newArray = (E[]) new Object[newCapacity];
// System.arraycopy(elements, 0, newArray, 0, size);
// elements = newArray;
}
}
}
测试用例:
package org.mobiletrain.test;
import org.mobiletrain.util.MyArrayList;
import org.mobiletrain.util.MyList;
class Test01 {
public static void main(String[] args) {
String[] strs = { "mango", "watermelon", "blueberry" };
MyList<String> myList = new MyArrayList<>(strs);
myList.add("apple");
myList.add("pitaya");
System.out.println(myList.size());
myList.add("mango");
myList.add("orange");
myList.add("mango");
myList.remove("apple");
myList.remove("mango", true);
System.out.println(myList.size());
myList.removeAtIndex(myList.size() - 1);
myList.add(new String[] { "grape", "durian" });
myList = myList.subList(1, 3);
System.out.println(myList.isEmpty());
for (int i = 0; i < myList.size(); ++i) {
System.out.println(myList.get(i));
}
myList.clear();
System.out.println(myList.size());
}
}