动态数组

作者: ZhuZongxing | 来源:发表于2019-12-31 18:58 被阅读0次
  • Java实现:
/**
 * 动态数组
 *
 * @param <E> 元素类型
 * @author ZhuZongxing
 */
public class Array<E> {
    private E[] mData;
    private int mSize;

    public Array(int capacity) {
        //TODO 类型强转不安全如何解决
        mData = (E[]) new Object[capacity];
        mSize = 0;
    }

    public Array() {
        this(10);
    }

    public int getSize() {
        return mSize;
    }

    public int getCapacity() {
        return mData.length;
    }

    public boolean isEmpty() {
        return mSize == 0;
    }

    public boolean add(int index, E e) {
        if (index < 0) {
            throw new IllegalArgumentException("非法index");
        }
        if (mSize == getCapacity()) {
            //扩容
            resize(2 * getCapacity());
        }
        for (int i = mSize - 1; i >= index; i--) {
            mData[i + 1] = mData[i];
        }
        mData[index] = e;
        mSize++;
        return true;
    }

    public E get(int index) {
        if (index < 0 || index >= mSize) {
            throw new IllegalArgumentException("非法index");
        }
        return mData[index];
    }

    public boolean set(int index, E e) {
        if (index < 0 || index >= mSize) {
            return false;
        }
        mData[index] = e;
        return true;
    }

    public boolean contains(E e) {
        for (E temp : mData) {
            if (temp == e) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据下标删除元素
     *
     * @param index 下标
     * @return 被删除的元素
     */
    public E remove(int index) {
        if (index < 0 || index >= mSize) {
            throw new IllegalArgumentException("非法index");
        }
        E ret = mData[index];
        for (int i = index; i < mSize - 1; i++) {
            mData[i] = mData[i + 1];
        }
        mSize--;
        mData[mSize] = null;
        //四分之一是防止复杂度震荡,即当数组满了的时候不断加减一个元素,会导致不停resize()导致性能变差
        if (mSize == getCapacity() / 4 && getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }
        return ret;
    }

    /**
     * 从数组中删除元素e(包括重复的)
     *
     * @param e 要删除的元素
     * @return 是否删除成功
     */
    public boolean removeAllElement(E e) {
        for (int i = mSize - 1; i >= 0; i--) {
            if (e == mData[i]) {
                remove(i);
            }
        }
        return true;
    }

    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < mSize; i++) {
            newData[i] = mData[i];
        }
        mData = newData;
    }
}

相关文章

  • 20_总结

    一、动态数组 普通动态数组 环形动态数组 接口设计 int size(){} // 元素的数量 boolean i...

  • C语言动态数组

    一维动态数组 二维动态数组

  • C语言 泛型动态数组

    泛型实现思路:万能指针void *动态数组实现思路:动态进行数组内存的扩容 realloc 泛型动态数组 数组可以...

  • C++ 动态顺序表的实现(更新中)

    动态数组与数组相似,但是动态数组的大小可以在运行时动态修改。动态数组的元素占用连续的内存块,一旦创建,就无法更改其...

  • 笨办法学C 练习34:动态数组

    练习34:动态数组 原文:Exercise 34: Dynamic Array 译者:飞龙 动态数组是自增长的数组...

  • VBA之数组

    数组的声明 一维数组 二维数组 动态数组

  • 数据结构大纲

    1、线性表 1.1、数组 1.1.1、简介 数组是一段连续的内存 1.1.2、动态数组 有动态扩容功能和动态缩容功...

  • 关于ArrayList

    ArrayList简介 ArrayList内部是以动态数组存放数据的,所谓动态数组,不是数组本身可以改变长度,而是...

  • 2018-11-28

    vector容器。 vector类称为向量类,实现了动态数组,用于元素数组动态变化的对象数组。同数组一样,vect...

  • C++ 创建动态二维数组

    有时候数组过大,栈放不下,可以利用动态分配生成动态数组 动态创建数组时一定要记得结束程序时释放内存。

网友评论

    本文标题:动态数组

    本文链接:https://www.haomeiwen.com/subject/qfdioctx.html