MyIterator
MyCollection
MyAbstractCollection
MyListIterator
MyList
MyAbstractList
public interface MyIterator<E> {
boolean hasNext();
E next();
void remove();
}
MyCollection
public interface MyCollection<E> extends MyIterator<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
MyIterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
boolean containsAll(MyCollection<?> c);
boolean addAll(MyCollection<? extends E> c);
boolean removeAll(MyCollection<?> c);
boolean retainAll(MyCollection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
}
MyAbstractCollection
public abstract class MyAbstractCollection<E> implements MyCollection<E> {
protected MyAbstractCollection() {
}
public abstract int size();
public boolean isEmpty() {
return size() == 0;
}
public boolean contains(Object o) {
MyIterator<E> e = iterator();
if (o == null) {
while (e.hasNext()) {
if (e.next() == null)
return true;
}
} else {
while (e.hasNext()) {
if (o.equals(e.next()))
return true;
}
}
return false;
}
public abstract MyIterator<E> iterator();
public Object[] toArray() {
MyIterator<E> e = iterator();
Object[] r = new Object[size()];
for (int i = 0; i < r.length; i++) {
if (!e.hasNext())
return java.util.Arrays.copyOf(r, i);
r[i] = e.next();
}
return e.hasNext() ? finishToArray(r, e) : r;
}
private static <T> T[] finishToArray(T[] r, MyIterator<?> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
int newCap = ((cap / 2) + 1) * 3;
if (newCap <= cap) { // integer overflow
if (cap == Integer.MAX_VALUE)
throw new OutOfMemoryError(
"Required array size too large");
newCap = Integer.MAX_VALUE;
}
r = java.util.Arrays.copyOf(r, newCap);
}
r[i++] = (T) it.next();
}
// trim if overallocated
return (i == r.length) ? r : java.util.Arrays.copyOf(r, i);
}
public boolean add(E e) {
// java.lang的一个异常
throw new UnsupportedOperationException();
}
public boolean remove(Object o) {
MyIterator<E> e = iterator();
if (o == null) {
while (e.hasNext()) {
if (e.next() == null) {
e.remove();
return true;
}
}
} else {
while (e.hasNext()) {
if (o.equals(e.next())) {
e.remove();
return true;
}
}
}
return false;
}
public boolean containsAll(MyCollection<?> c) {
MyIterator<?> e = c.iterator();
while (e.hasNext()) {
if (!contains(e.next()))
return false;
}
return true;
}
public boolean addAll(MyCollection<? extends E> c) {
boolean b = false;
MyIterator<? extends E> e = c.iterator();
while (e.hasNext()) {
if (add(e.next()))
b = true;
}
return b;
}
public boolean removeAll(MyCollection<?> c) {
boolean b = false;
MyIterator<?> e = iterator();
while (e.hasNext()) {
if (c.contains(e.next())) {
e.remove();
b = true;
}
}
return b;
}
public boolean retainAll(MyCollection<?> c) {
boolean b = false;
MyIterator<?> e = iterator();
while (e.hasNext()) {
if (!c.contains(e.next())) {
e.remove();
b = true;
}
}
return b;
}
public void clear() {
MyIterator<E> e = iterator();
if (e.hasNext()) {
e.next();
e.remove();
}
}
public String toString() {
MyIterator<E> i = iterator();
if (!i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = i.next();
sb.append(e == this ? "(this Collection)" : e);
if (!i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}
}
MyListIterator
public interface MyListIterator<E> {
boolean hasNext();
E next();
boolean hasPrevious();
E previous();
int nextIndex();
int previousIndex();
void remove();
void set(E e);
void add(E e);
}
MyList
public interface MyList<E> extends MyCollection<E> {
// 以下所有方法都是在MyList接口中新定义的
boolean addAll(int index, MyCollection<? extends E> c);
E get(int index);
E set(int index, E element);
void add(int index, E element);
E remove(int index);
int indexOf(Object o);
int lastIndexOf(Object o);
MyListIterator<E> listIterator();
MyListIterator<E> listIterator(int index);
MyList<E> subList(int fromIndex, int toIndex);
}
MyAbstractList
public abstract class MyAbstractList<E> extends MyAbstractCollection<E>
implements MyList<E> {
protected MyAbstractList() {
};
// MyList中方法
public boolean addAll(int index, MyCollection<? extends E> c) {
boolean b = false;
MyIterator<? extends E> e = c.iterator();
while (e.hasNext()) {
add(index++, e.next());
b = true;
}
return b;
}
// MyList中方法
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
// MyCollection中的方法,size()是MyCollection中的方法
public boolean add(E e) {
add(size(), e);
return true;
}
// MyList中方法
public abstract E get(int index);
// MyList中方法
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
// MyList中方法
public E remove(int index) {
throw new UnsupportedOperationException();
}
// MyList中方法
public int indexOf(Object o) {
MyListIterator<E> e = listIterator();
if (o == null) {
while (e.hasNext()) {
if (e.next() == null)
return e.previousIndex();
}
} else {
while (e.hasNext()) {
if (o.equals(e.next()))
return e.previousIndex();
}
}
return -1;
}
// MyList中方法
public int lastIndexOf(Object o) {
MyListIterator<E> e = listIterator(size());
if (o == null) {
while (e.hasPrevious())
if (e.previous() == null)
return e.nextIndex();
} else {
while (e.hasPrevious())
if (o.equals(e.previous()))
return e.nextIndex();
}
return -1;
}
// MyList中方法
public MyListIterator<E> listIterator() {
return listIterator(0);
}
public MyListIterator<E> listIterator(final int index) {
if (index < 0 || index > size())
throw new IndexOutOfBoundsException("Index: " + index);
return new ListItr(index);
}
}