MyList<E>接口:
package org.mobiletrain.util;
public interface MyList<E> {
/**
* 向容器中添加一個(gè)元素
* @param element
*/
public void add(E element);
/**
* 向容器中添加一組元素
* @param arrayOfElements 元素的數(shù)組
*/
public void add(E[] arrayOfElements);
/**
* 刪除指定的元素(首次出現(xiàn)的位置)
* @param e 待刪除的元素
* @return 如果元素存在返回true否則返回false
*/
public boolean remove(E e);
/**
* 刪除指定的元素
* @param e 元素
* @param allOccurence 如果為true則刪除所有位置上的該元素否則只刪除首次出現(xiàn)的位置
* @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);
/**
* 查找元素在容器中首次出現(xiàn)的位置
* @param element 元素
* @return 找到了返回元素首次出現(xiàn)的位置(索引)否則返回-1
*/
public int indexOf(E element);
/**
* 獲取指定位置的元素
* @param index 元素的位置(索引)
* @return 元素
*/
public E get(int index);
/**
* 用指定的位置獲取當(dāng)前容器的子容器
* @param fromIndex 起始位置(包含)
* @param toIndex 終止位置(不包含)
* @return 子容器
*/
public MyList<E> subList(int fromIndex, int toIndex);
/**
* 是不是空容器
* @return 容器沒有元素返回true否則返回false
*/
public boolean isEmpty();
/**
* 清空容器
*/
public void clear();
/**
* 容器的大小
* @return 容器中元素的個(gè)數(shù)
*/
public int size();
}
MyArrayList<E>去實(shí)現(xiàn)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("列表容器的容量不能負(fù)數(shù)");
}
}
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;
}
}
}
測(cè)試用例:
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());
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者