Collection接口设计
public interface Collection<E> {
int ELEMENT_NOT_FOUNT = -1;
int size();
boolean isEmpty();
boolean contains(E element);
void add(E element);
void add(int index, E element);
boolean remove(E element);
E remove(int index);
void clear();
E get(int index);
E set(int index, E element);
int indexOf(E element);
}
List接口继承Conllection接口
public interface List<E> extends Collection<E> {
}
AbstractList抽象类实现List接口
/**
* @author feng
* @date 2020/12/20
* @Description
*/
public abstract class AbstractList<E> implements List<E> {
protected int size;
@Override
public int size() {
return size;
}
@Override
public void add(E element) {
add(size,element);
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public boolean contains(E element) {
return indexOf(element) != ELEMENT_NOT_FOUNT;
}
protected void outOfBounds(int index){
throw new IndexOutOfBoundsException("index:" + index + "size" + size);
}
protected void rangeCheck(int index){
if (index < 0 || index >= size){
outOfBounds(index);
}
}
protected void rangeCheckForAdd(int index){
if (index < 0 || index > size){
outOfBounds(index);
}
}
}
循环数组CircleArray<E>
import java.util.Arrays;
import java.util.Comparator;
/**
* @author feng
* @date 2020/12/22
* @Description
*/
@SuppressWarnings("unchecked")
public class CircleArray<E> extends AbstractList<E> {
/**
* 记录头元素的位置
*/
private int front;
/**
* 元素的个数
*/
private E[] elements;
/**
* 默认初始容量
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* 比较器
*/
private Comparator<E> comparator;
/**
* 随便加的为了toString的分组输出
*/
private double group = size;
public CircleArray(Comparator<E> comparator) {
this.comparator = comparator;
}
public CircleArray(double