数组

作者: 你好星期四 | 来源:发表于2018-04-26 21:06 被阅读14次
数组1.png 数组2.png 数组3.png

Array.java

public class Array<E> {
    private E[] data;
    private int size;

    // 构造函数,传入数组的容量capacity构造Array
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    // 默认构造函数
    public Array() {
        this(10);
    }

    // 返回数组的元素个数
    public int getSize() {
        return size;
    }

    // 返回数组的容量
    public int getCapacity() {
        return data.length;
    }

    // 判断数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 向数组开头插入元素
    public void unshift(E e) {
        add(0, e);
    }

    // 向所有元素后添加一个新元素
    public void append(E e) {
        add(size, e);
    }

    // 在第index个位置插入一个新元素e
    public void add(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Require index>=0 and index<= size");
        if (size == data.length)
            resize(2 * data.length);

        // 把index之后的元素向后移一位
        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;
    }

    // 获取index索引位置
    E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }

    // 设置index位置的值
    void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        data[index] = e;
    }

    // 查找数组中是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    // 查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    // 从数组中删除index位置的元素,返回删除的元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        E ret = data[index];

        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];

        size--;
        data[size] = null; // loitering objects != memory leak
        if (size == data.length / 4 && data.length / 2 != 0) // Lazy,防止 append、pop依次频繁调用导致复杂度震荡
            resize(data.length / 2);
        return ret;
    }

    // 删除数组中第一个元素
    public E shift() {
        return remove(0);
    }

    // 删除数组中最后一个有效位置对应的元素
    public E pop() {
        return remove(size - 1);
    }

    // 删除指定的元素
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array:size = %d , capacity = %d\n", size, data.length));
        sb.append('[');
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if (i != size - 1)
                sb.append(", ");
        }
        sb.append("]");
        return sb.toString();
    }

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

Main.java

public class Main {

    public static void main(String[] args) {
        // write your code here
        Array<Integer> arr = new Array<>();
        for (int i = 0; i < 10; i++) {
            arr.append(i);
        }
        System.out.println(arr);
        arr.add(1, 100);
        System.out.println(arr);
        arr.unshift(-1);
        System.out.println(arr);
        arr.remove(2);
        System.out.println(arr);
        arr.removeElement(4);
        System.out.println(arr);
        arr.shift();
        System.out.println(arr);
    }
}

output

Array:size = 10 , capacity = 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:size = 11 , capacity = 20
[0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:size = 12 , capacity = 20
[-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:size = 11 , capacity = 20
[-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:size = 10 , capacity = 20
[-1, 0, 1, 2, 3, 5, 6, 7, 8, 9]
Array:size = 9 , capacity = 20
[0, 1, 2, 3, 5, 6, 7, 8, 9]

相关文章

  • 数组

    数组数组数组数组数组数组数组数组数组

  • JavaScript - 5.数组<增删改查>

    数组 Array 数组 - 增 数组 - 删 / 改 数组 - 查 数组 - 自动 toString() 数组 -...

  • PHP数组使用

    数组定义 数组增、删、改 数组查询 数组排序 数组合并、分割 数组比较、去重复 数组长度 数组遍历 数组转换 其他...

  • 》》》PHP初入---(三)

    数组定义 1.索引数组:数组下标是整型的 声明数组: 访问数组: count(数组)--获取数组长度 查看数组所有...

  • JavaScript中数组的常用操作

    数组的遍历 数组的映射 数组的简化 数组的连接 获取数组的片段 数组的拷贝 查找数组 数组去重

  • JavaSE之数组

    六、数组 目录:数组概述、数组声明创建、数组使用、多维数组、Array类、稀疏数组 1.什么是数组 数组的定义:数...

  • Shell数组、关联数组

    数组 定义数组 获取数组 关联数组 定义关联数组 获取关联数组

  • 学习Java第五天

    数组是多个数据的集合 数组的语法 数组元素类型【】 数组名; 多维数组: 数组元素类型【】【】 数组名; 多维数组...

  • php基础精粹

    PHP php数组 php数组之索引数组初始化 PHP数组之索引数组赋值 PHP数组之访问索引数组内容 PHP数组...

  • C语言的惯用集

    数组部分 数组部分 清空数组a 把数据读进数组a 对数组a求和

网友评论

      本文标题:数组

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