接口:抽象,描述数组的基本行为
抽象类:抽象,将固定的写在此,将无法确定的,给到子类
实现类:动态、静态
接口:
/**
* 抽象,描述数组的基本行为
* @param <E>
*/
public interface Array<E> {
// -------------对数组的修改操作-------------
E add(E element);
E remove(E element);
E updata(E element);
//-------------对数组的属性操作-------------
int getCapacity();
int getSize();
boolean isEmpty();
boolean isFull();
void printArr();
}
抽象类:
/**
* 抽象,将固定的写在此,将无法确定的,给到子类
* @param <E>
*/
public abstract class AbstractArray<E> implements Array<E> {
// 默认数组大小
protected static final int DEFAULT_CAPACITY = 10;
protected int size;
protected E[] arr;
protected AbstractArray(int capacity){
this.arr = (E[]) new Object[capacity];
}
/**
* 子类负责处理满了之后的策略,父类中不清楚子类要怎么处理
*/
protected abstract void handleFullyArray();
@Override
public E add(E element) {
if (isFull()){
// 这里如果满了,要做个处理,至于处理是什么,暂时不指定
handleFullyArray();
}
int index = this.size++;
this.arr[index] = element;
return element;
}
@Override
public E remove(E element) {
if (isEmpty()){
// 空
throw new IllegalStateException("空了不能删");
}
for (int i = 0; i < this.size - 1; i++) {
if (this.arr[i].equals(element)) {
for (int j = i; j < this.size - 1; j++) {
this.arr[j] = this.arr[j + 1];
}
}
}
if(this.size-- < this.getCapacity() / 2){
handleReduceArray();
}
return element;
}
//
protected abstract void handleReduceArray();
@Override
public E updata(E element) {
return null;
}
@Override
public int getCapacity() {
return this.arr.length;
}
@Override
public int getSize() {
return this.size;
}
@Override
public boolean isEmpty() {
return this.size == 0;
}
@Override
public boolean isFull() {
return this.size == this.arr.length;
}
@Override
public void printArr() {
for (E e : this.arr) {
if (e != null){
System.out.println(e);
}
}
}
}
动态数组:
public class DynamicArray<E> extends AbstractArray<E>{
private DynamicArray(){
super(DEFAULT_CAPACITY);
}
private DynamicArray(int capacity){
super(capacity);
}
public static <E> DynamicArray<E> of(){
return new DynamicArray<>();
}
public static <E> DynamicArray<E> of(int capacity){
return new DynamicArray<>(capacity);
}
@Override
protected void handleFullyArray() {
// TODO 这里的扩容是遍历,实际用System.arraycopy();效果更佳
Integer newCapacity = this.getCapacity() * 2;
if(newCapacity >= 2048){
throw new IllegalStateException("");
}
E[] buf = (E[]) new Object[this.getCapacity() * 2];
for (int i = 0; i < this.size; i++) {
buf[i] = this.arr[i];
}
this.arr = buf;
}
@Override
protected void handleReduceArray() {
Integer newCapacity = Math.max(DEFAULT_CAPACITY, this.getCapacity() / 2);
E[] buf = (E[]) new Object[newCapacity];
for (int i = 0; i < this.size; i++) {
buf[i] = this.arr[i];
}
this.arr = buf;
}
}
静态数组:
public class FixedArray<E> extends AbstractArray<E>{
FixedArray(){
super(DEFAULT_CAPACITY);
}
private FixedArray(int capacity){
super(capacity);
}
public static <T> FixedArray<T> of(){
return new FixedArray<>();
}
public static <T> FixedArray<T> of(int capacity){
return new FixedArray<>(capacity);
}
@Override
protected void handleFullyArray() {
throw new IllegalStateException("数组满了");
}
@Override
protected void handleReduceArray() {
return;
}
}