java用數組模擬實現ArrayList以及一些常用方法實現

package com.yys.student;

/**
 * Created by yys on 2017/5/4.
 */
public class SxtArrayList {
    private Object[] elementDate;
    private int size;
    /**
     * 默認無參構造方法 SxtArrayList()
     */
    public SxtArrayList(){
    this(10);
    }
    /**
     * 默認帶參構造方法 SxtArrayList(int initialCapacity)
     */
    public SxtArrayList(int initialCapacity){
        if(initialCapacity<0){
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        elementDate = new Object[initialCapacity];
    }
    /**
     * 返回list大小 size()
     * 返回SxtArrayList對象大小即SxtArrayList中數組大小
     */
    public int size(){
        return size;
    }
    /**
     * 判斷list是否爲空 isEmpty() 此方法待改進 初始化時並未使用size 只有add時候用到size屬性 直接判斷size不正確
     * 返回SxtArrayList對象是否爲空即SxtArrayList中數組中是否有實際值
     */
    public boolean isEmpty(){
        return size == 0;
    }
    /**
     * 新增對象 add(Object obj)
     * ensureCapacity()檢查數組是否需要數組擴容和數據拷貝 後添加元素
     * 返回 null
     */
    public void add(Object obj){
        //數組擴容和數據拷貝
        ensureCapacity();
        elementDate[size++] = obj;
    }
    /**
     * 指定位置新增對象 原來此位置對象後移 add(int index , Object object)
     * rangeCheck(index)檢驗數組下標是否越界
     * ensureCapacity()檢查數組是否需要數組擴容和數據拷貝
     * 然後進行原來數組內容向後移動
     * 最後把新值傳入相應位置
     * 數組長度++
     * 返回 null
     */
    public void add(int index , Object object){
        //檢驗數組下標是否越界
        rangeCheck(index);
        //數組擴容和數據拷貝
        ensureCapacity();
        //第一個是要複製的數組,第二個是從要複製的數組的第幾個開始,
        //第三個是複製到那,四個是複製到的數組第幾個開始,最後一個是複製長度
        System.arraycopy(elementDate,index,elementDate,index+1 ,size-index);
        elementDate[index] = object;
        size++;
    }
    /**
     * 獲取指定位置對象 get()
     * rangeCheck(index)檢驗數組下標是否越界
     * 返回數組對應值(object)
     */
    public Object get(int index){
        rangeCheck(index);
        return elementDate[index];
    }
    /**
     * 刪除指定位置對象 remove(int index)
     * rangeCheck(index)檢驗數組下標是否越界
     * 刪除對象前計算出要刪除的對象後面還剩多少對象以便於後面元素向前移動
     * 移動對象
     * 原數組最後一位設置爲空
     * 如果刪除對象在數組最後一位則直接刪除無需數組元素移動
     * 返回 null
     */
    public void remove(int index){
        rangeCheck(index);
        int numMove = size - index -1;
        if(numMove > 0){
            //第一個是要複製的數組,第二個是從要複製的數組的第幾個開始,
            //第三個是複製到那,四個是複製到的數組第幾個開始,最後一個是複製長度
            System.arraycopy(elementDate,index +1,elementDate,index,numMove);
            elementDate[--size] = null;
        }else if(numMove == 0){
            elementDate[--size] = null;
        }
    }
    /**
     * 重新設置指定位置對象 set(int index,Object object)
     * rangeCheck(index)檢驗數組下標是否越界
     * 獲取對應下標對象 暫定爲舊對象
     * 對應下標對象重新賦值
     * 返回 舊對象
     */
    public Object set(int index,Object object){
        rangeCheck(index);
        Object oldValue = elementDate[index];
        elementDate[index] = object;
        return oldValue;
    }
    /**
     * 刪除指定對象 remove(Object obj)
     * 判斷傳入對象是否存在
     * 如果存在則會得到相應下標位置
     * 利用下標位置刪除此元素
     * 返回 null
     */
    public void remove(Object obj){
        for(int i=0;i<size;i++){
            if(get(i).equals(obj)){//底層是equls
                remove(i);
            }
        }
    }
    /**
     * 檢驗數組下標是否越界
     */
    private void rangeCheck(int index){
        if(index<0 || index>=size){
            try {
                throw new Exception("下標越界");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 數組擴容和數據拷貝
     */
    private void ensureCapacity(){
        if(size==elementDate.length){
            Object[] newArray = new Object[size*2+1];
            System.arraycopy(elementDate,0,newArray,0,elementDate.length);
//            System.arraycopy()代替for循環
//            for(int i=0;i<elementDate.length;i++){
//                newArray[i] = elementDate[i];
//            }
            elementDate = newArray;
        }
    }
    public static void main(String args[]){
        SxtArrayList sxtArrayList = new SxtArrayList(1);
        sxtArrayList.add("3");
        sxtArrayList.add(0,"2");
        sxtArrayList.add(0,"1");
        sxtArrayList.set(2,"4");
        for(int i=0;i<sxtArrayList.size();i++){
            System.out.println(sxtArrayList.get(i));
        }
        sxtArrayList.remove(0);
        sxtArrayList.remove("4");
        System.out.println("執行 remove(0) 和remove(\"4\")後剩餘元素");
        for(int i=0;i<sxtArrayList.size();i++){
            System.out.println(sxtArrayList.get(i));
        }
        sxtArrayList.remove(0);
        System.out.println("執行 remove(0)否爲空  "+sxtArrayList.isEmpty());
    }
}
發佈了45 篇原創文章 · 獲贊 36 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章