Don't look down on arrays

Keywords: Programming

Definition

An array is an ordered sequence of elements. If you name a collection of a limited number of variables of the same type, the name is an array name. The variables that make up an array are called the components of the array, also called the elements of the array, and sometimes called subscript variables. The number used to distinguish the elements of an array is called a subscript. Array is a kind of form in which several elements of the same type are organized in an unordered form for the convenience of processing. These unordered collections of like data elements are called arrays.

code implementation

package com.company;

public class Array<E> {
    // Generic array
    private E[] data;
    // Number of storage elements
    private int size;

    // Constructor
    public Array(){
        this(10);
    }

    //Constructor
    public Array(int capacity){
        data = (E[]) new Object[capacity];
        size = 0;
    }

    // Get the number of elements in the array
    public int getSize() {
        return size;
    }

    // Get array capacity
    public int getCapacity(){
        return data.length;
    }

    // Judge whether the array is empty
    public boolean isEmpty(){
        return size == 0;
    }

    // Insert element to array header
    public void addFirst(E elem){
        for (int i = size; i >= 0; i--) {
            data[i + 1] = data[i];
        }
        data[0] = elem;
        size++;
    }

    // Add element to end of array
    public void addLast(E elem){
        if (size == data.length){
            resize(data.length * 2);
        }
        data[size++] = elem;
    }

    // Insert element to specified location
    public void add(int index, E elem){
        if (size == data.length){
            resize(data.length * 2);
        }
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Illegal insertion position");
        }
        for (int i = size - 1; i > index - 1; i--) {
            data[i + 1] = data[i];
        }
        data[index] = elem;
        size++;
    }

    // Get the element corresponding to the index
    public E get(int index){
        if (index < 0 || index >= size){
            throw new IllegalArgumentException("Array Index Overflow");
        }
        return data[index];
    }

    // Get first element
    public E getFirst(){
        return get(0);
    }

    // Get last element
    public E getLast(){
        return get(size - 1);
    }

    // Modify the element corresponding to the index
    public void set(int index, E elem){
        if (index < 0 || index >= size){
            throw new IllegalArgumentException("Illegal insertion position");
        }
        data[index] = elem;
    }

    // Determine whether the element exists
    public boolean contains(E elem){
        for (int i = size; i > 0; i--) {
            if (data[i].equals(elem)){
                return true;
            }
        }
        return false;
    }

    // Find the index corresponding to the element
    public int find(E elem){
        for (int i = 0; i < size; i++) {
            if (data[i].equals(elem)){
                return i;
            }
        }
        return -1;
    }

    // Delete elements of the specified index
    public E remove(int index){
        E ret = data[index];
        for (int i = index;i < size;i++){
            data[i] = data[i+1];
        }
        size--;
        data[size] = null;
        if (size == data.length / 2 && data.length /2 != 0){
            resize(data.length / 2);
        }
        return ret;
    }

    // Remove element from header
    public E removeFirst(){
        return remove(0);
    }

    // Remove element from tail
    public E removeLast(){
        return remove(size -1);
    }

    // Delete specified element
    public void removeElem(E elem){
        int index = find(elem);
        if (index != -1)
        {
            remove(index);
        }
    }

    // Modify array capacity
    private void resize(int newCapacity){
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

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

Posted by Alex-B on Sat, 30 Nov 2019 23:33:46 -0800