Java 动态数组基础版

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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值