package org.example.datastructure.dynamicarray;
import java.util.Arrays;
import java.util.Iterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class DynamicArray<T> implements Iterable<T> {
// 逻辑大小
private int size = 0;
// 容量
private int capacity = 8;
private T[] array;
public DynamicArray() {
this.array = (T[]) new Object[capacity];
}
/**
* 向最后添加元素
*
* @param element
*/
public void add(T element) {
checkAndGrow(size + 1);
if (this.size + 1 > capacity) {
throw new IndexOutOfBoundsException();
} else {
array[this.size] = element;
this.size++;
}
}
/**
* 给指定位置添加元素
*
* @param index
* @param element
*/
public void add(int index, T element) {
checkAndGrow(size + 1);
if (index > this.size || index < 0) {
throw new IndexOutOfBoundsException();
} else {
System.arraycopy(array, index, array, index + 1, this.size - index);
array[index] = element;
this.size++;
}
}
/**
* 判断是否需要扩容
*/
private void checkAndGrow(int newSize) {
if (newSize == this.capacity) {
resizeExpand(this.capacity * 2);
}
if (newSize < capacity / 4) {
resizeShrink(capacity / 2);
}
}
/**
* 修改array数组长度
* 扩容
*
* @param newCapacity
*/
private void resizeExpand(int newCapacity) {
T[] newArray = (T[]) new Object[newCapacity];
capacity = newCapacity;
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
/**
* 修改array数组长度
* 缩容
*
* @param newCapacity
*/
private void resizeShrink(int newCapacity) {
T[] newArray = (T[]) new Object[newCapacity];
capacity = newCapacity;
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
/**
* 获取指定位置的元素
*
* @param index
* @return
*/
public T get(int index) {
if (index > size || index < 0) {
throw new IndexOutOfBoundsException();
} else {
return array[index];
}
}
/**
* 查找元素第一次出现的位置
*
* @param element
* @return
*/
public int indexOf(T element) {
if (element == null) {
for (int i = 0; i < this.size; i++) {
if (array[i] == null) {
return i;
}
}
} else {
for (int i = 0; i < this.size; i++) {
if (element == array[i]) {
return i;
}
}
}
return -1;
}
/**
* 删除元素并且返回删除元素
*
* @param index
* @return
*/
public T remove(int index) {
checkAndGrow(size);
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
T removed = array[index];
System.arraycopy(array, index + 1, array, index, size - index - 1);
size--;
return removed;
}
@Override
public String toString() {
return Arrays.toString(stream().toArray());
}
/**
* 接口遍历
*
* @param consumer
*/
public void foreach(Consumer<T> consumer) {
for (int i = 0; i < size; i++) {
consumer.accept(array[i]);
}
}
/**
* 迭代器遍历
*
* @return
*/
@Override
public Iterator<T> iterator() {
return new Iterator() {
int i = 0;
/**
* 判断有没有下一个元素
* @return
*/
@Override
public boolean hasNext() {
return i < size;
}
/**
* 返回当前元素
* 并且移动到下一个元素
* @return
*/
@Override
public T next() {
return array[i++];
}
};
}
/**
* 流遍历
*
* @return
*/
public Stream<T> stream() {
return Stream.of(Arrays.copyOfRange(array, 0, size));
}
/**
* @return
*/
public int getSize() {
return size;
}
/**
* //判断集合是否为空
*
* @return
*/
public boolean isEmpty() {
return size == 0;
}
}
Java 动态数组基础版
于 2024-01-19 20:23:47 首次发布