一、Array 数组

数组有上界和下界,数组的元素在上下界内是连续的。
数组的特点是:数据是连续的;随机访问速度快。数组中稍微复杂一点的是多维数组和动态数组。至于动态数组,是指数组的容量能动态增长的数组;Collection集合中提供了ArrayList和Vector。


package net.good.spring;

public class Array {

    private E[] data;
    private int size;

    // Array构造函数。capacity初始化容量大小
    public Array(int capacity) {
        if (capacity < 0) {
            throw new IllegalArgumentException("Illegal Capacity: " + capacity);
        }
        this.data = (E[]) new Object[capacity];
        this.size = 0;
    }

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

    // 获取数组中个元素个数
    public int getSize() {
        return this.size;
    }

    // 返回数组是否为空
    public boolean isEmpty() {
        return this.size == 0;
    }

    // 获取数组的容量
    public int getCapacity() {
        return this.data.length;
    }

    // 在第index个位置插入一个新的元素e
    public E[] add(int index, E e) {
        checkAddRange(index);
        ensureCapacity(this.size + 1);
        System.arraycopy(this.data, index, this.data, index + 1, this.size - index);
        this.data[index] = e;
        this.size++;
        return this.data;
    }

    // 插入一个新的元素e
    public E[] add(E e) {
        return add(this.size, e);
    }

    // 确定Arrar的容量。
    public void ensureCapacity(int capacity) {
        if (capacity >= getCapacity()) {
            // 若当前容量不足以容纳当前的元素个数,设置 新的容量=“(原始容量x2) + 1”
            int newCapacity = getCapacity() * 2 + 1;
            if (capacity > newCapacity) {
                newCapacity = capacity;
            }
            E[] newData = (E[]) new Object[newCapacity];
            System.arraycopy(this.data, 0, newData, 0, this.size);
            this.data = newData;
        }
    }

    public void checkAddRange(int index) {
        if (index < 0 || index > this.size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
        }
    }

    public void checkRange(int index) {
        if (index < 0 || index >= this.size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size);
        }
    }

    // 获取index位置的元素值
    public E get(int index) {
        checkRange(index);
        return this.data[index];
    }

    // 设置index位置的值为element
    public void set(int index, E e) {
        checkRange(index);
        this.data[index] = e;
    }

    //是否包含element
    public boolean contains(E e) {
        return indexOf(e) >= 0;
    }

    // 正向查找,返回元素的索引值
    public int indexOf(E e) {
        if (e == null) {
            for (int i = 0; i < this.size; i++) {
                if (this.data[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = 0; i < this.size; i++) {
                if (e.equals(this.data[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    // 反向查找,返回元素的索引值
    public int lastIndexOf(E e) {
        if (e == null) {
            for (int i = this.size - 1; i >= 0; i--) {
                if (this.data[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = this.size - 1; i >= 0; i--) {
                if (e.equals(this.data[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    //删除指定index的元素
    public E remove(int index) {
        checkRange(index);
        E oldValue = this.data[index];
        int numMoved = size - index - 1;
        if (numMoved > 0){
            System.arraycopy(this.data, index+1, this.data, index, numMoved);
        }
        this.data[--this.size] = null;
        return oldValue;
    }

    //删除指定元素
    public int remove(E e){
        int index = indexOf(e);
        if (index > 0) {
            remove(index);
        } 
        return index;
    }

    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append('[');
        for(int i = 0; i < size; i++) {
            res.append(data[i]);
            if(i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }
}

参考:skywang12345
哈哈大圣

你可能感兴趣的:(一、Array 数组)