抛砖引玉

本文详细解析了一种自定义的集合框架实现,包括接口定义如MyIterator、MyCollection及其抽象基类MyAbstractCollection等,同时介绍了自定义列表接口MyList及其实现类MyAbstractList的相关方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyIterator
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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值