Skip to content

Latest commit

 

History

History
146 lines (116 loc) · 3.26 KB

手写动态数组.md

File metadata and controls

146 lines (116 loc) · 3.26 KB

package demo;

import java.util.Arrays;

public class AutoArry { private int size; private int capacity; private T[] arry;

public AutoArry() {
    size = 16;
    capacity = 16;
    arry = (T[]) new Object[size];
}


private boolean resize(int NewCapacity) {

    T[] newArry = (T[]) new Object[NewCapacity];
    for (int i = 0; i < size; i++) {
        newArry[i] = arry[i];
    }
    arry = newArry;
    this.capacity = NewCapacity;
    return true;
}

public boolean is_empty() {
    if (this.size == 0 || this.arry[0] == null) {
        return true;

    }
    return false;
}

public Object at(int index) {
    if (index > size || index < 0) {
        return -1;
    } else {
        for (int i = 0; i < size; i++) {
            if (i == index) {
                return arry[i];
            }
        }
    }

    return arry[index];
}

public boolean push(T item) {
    if (this.is_empty()) {
        arry[0] = item;
        this.size = 1;
        return true;
    } else {
        if (size >= capacity) {
            this.resize(size * 2);
        }

        for (int j = size; j >= 1; j--) {
            arry[j] = arry[j - 1];
        }
        arry[0] = item;
        size++;
    }

    return true;
}

public int insert(int index, T nums) {
    if (index > size || index < 0) {
        return -1;
    } else if (size == this.capacity) {
        this.resize(size * 2);
    }

    for (int i = size; i > index; i--) {
        arry[i] = arry[i - 1];
        if (index == i - 1) {
            arry[index] = nums;
            size++;
        }
    }

    return 1;
}

public void pop() {

}

public void delete(int index) {

}

public int remove(int value) {
    return -1;
}

public int find(int value) {
    return -1;
}


public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

public int getCapacity() {
    return capacity;
}

public void setCapacity(int capacity) {
    this.capacity = capacity;
}

public static void main(String[] args) {
    AutoArry autoArry = new AutoArry();
    System.out.println(autoArry.is_empty());
    System.out.println(autoArry.push(1));
    System.out.println(autoArry.push(2));
    System.out.println(autoArry.push(3));
    System.out.println(autoArry.push(4));
    System.out.println(autoArry.push(5));
    System.out.println(autoArry.push(6));
    System.out.println(autoArry.push(7));
    System.out.println(autoArry.push(8));
    System.out.println(autoArry.push(9));
    System.out.println(autoArry.push(10));
    System.out.println(autoArry.push(11));
    System.out.println(autoArry.push(12));
    System.out.println(autoArry.push(13));
    System.out.println(autoArry.push(14));
    System.out.println(autoArry.push(15));
    System.out.println(autoArry.push(16));
    System.out.println(autoArry.insert(1, 17));
    System.out.println(autoArry.size);
    System.out.println(autoArry.at(1));


}

}