1. 程式人生 > >JAVA:MyArraylist泛型自己實現

JAVA:MyArraylist泛型自己實現

MyArraylist :

public class MyArraylist <T> {
    private T[] arr;
    private int size;
    private static final int CAPACITY = 10;

    public MyArraylist() {
        this(CAPACITY);
    }

    public int getSize() {
        return size;
    }

    public MyArraylist(int n) {//判斷n是否符合要求,需要異常處理
        size = 0;
        if (n < 0) {
            try {
                throw new Exception();
            } catch (Exception e) {
                System.out.println(n + "不能為負數");
            }
        }
        arr = (T[]) new Object[n];
    }

    private void grow() {
        int oldCapacity = arr.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        arr = Arrays.copyOf(arr, newCapacity);
    }

    private void isfull() {
        if (arr.length == size) {
            grow();
        } else {
            return;
        }
    }

    /**
     * 增:
     * 1.直接增加,等同尾插
     * 2.頭插
     * 3.按照位置插入
     * 存在處理問題:判滿;擴容;按照位置插入時需要判斷index的範圍
     * size++
     */
    public void add(T value) {
        isfull();
        arr[size++] = value;
    }

    public void addTead(T value) {
        isfull();
        System.arraycopy(arr, 0, arr, 1, size);
        arr[0] = value;
    }//用拷貝比用for迴圈平移時間複雜度低

    public void addTail(T value) {
        isfull();
        arr[size++] = value;
    }

    private void rangeCheck(int index) {
        if (index < 0 || index > arr.length) {
            return;
        }
    }

    public void addIndex(int index, T value) {//按索引插入
        rangeCheck(index);
        isfull();
        System.arraycopy(arr, index, arr, index + 1, size - index);
        arr[index] = value;
        size++;
    }

    /**
     * 刪:
     * 1.按照位置刪
     * 2.按照值刪  ----->涉及查該值是否存在返回位置
     * 3.頭刪
     * 4.尾刪
     * size--;
     * 記憶體洩漏null
     * 檢查範圍
     *
     */
    public T[] delIndex(int index) {//按照位置刪
        rangeCheck(index);
        System.arraycopy(arr, index+1, arr, index , size - index);
        arr[size] = null;//防止記憶體洩漏
        size--;
        return arr;
    }
    public T[] delValue(T value){//按照值刪
        int pos=findValue(value);
        if(pos!=-1){
            System.arraycopy(arr, pos+1, arr, pos, size - pos);
            arr[size]=null;
            size--;
        }
        return arr;
    }
    public void delHead(){//頭刪
        System.arraycopy(arr, 1, arr, 0, size-1);
        arr[size]=null;
        size--;
    }
    public void delTail(){//尾刪
        arr[size]=null;
        size--;
    }

    /**
     * 改:
     * 按照位置改
     */
    public void  change(int index,T value){//按照位置改
        rangeCheck(index);
        arr[index]=value;
    }

    /**
     * 查:
     * 按照值查詢---》返回位置
     * 按照位置查詢-----》返回值
     *
     */
    public int findValue(T value){//按照值查詢
        for(int i=0;i<size;i++){
            if(arr[i].equals(value)){
                return i;
            }
        }
        return -1;
    }
    public T findIndex(int index){
        rangeCheck(index);
        return arr[index];
    }

    /**
     * 顯示
     */
    public void  show(){
        for(int i=0;i<size;i++){
            System.out.println(arr[i].toString());
        }
    }
}

People類:

class People{
    private String name;
    private int id;

    public People(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

主函式:main

 public static void main(String args[]) {
        MyArraylist<People> list = new MyArraylist<People>(3);
        People p1 = new People("zs", 002);
        People p2 = new People("ls", 003);
        list.add(p1);
        list.add(p2);
        list.delValue(p1);
        list.show();
        System.out.println();
//        list.show();
        list.delIndex(1);
//        System.out.println(list.size());
//        list.show();
        System.out.println(list.findIndex(0));
//        System.out.println(list.getSize());


    }