JAVA 用數組實現 ArrayList

來源:互聯網
上載者:User

標籤:object   java   public   元素   

1、能自動擴容

  2、能存放不同類型的資料

這兩點我們是這樣解決的:

  1、當一個資料存放滿了,我們就將這個資料複製到一個新的數組中,而這個新的數組容量要比原數組大。通過這樣不斷的擴大數組長度,也就是集合的容量。那麼這裡我們用到了這個方法  System.arraycopy

完整的寫法為:public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

參數

@ src -- 這是源數組 @ srcPos -- 這是源數組中的起始位置 @dest -- 這是目標數組 @ destPos -- 這是目標資料中的起始位置  @ length -- 這是一個要複製的數組元素的數目

1234 int arr1[] = {0,1,2,3,4,5};int arr2[] = {0,10,20,30,40,50};System.arraycopy(arr1,0,arr2,1,2);//結果為:arr2 = [0,0,1,30,40,50];

  2、第二個問題,我們只需要聲明為 Object 類型的數組就可以了。

 

 

完整代碼如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 package com.ys.collection; public class MyArrayList {    //用於儲存資料    private transient Object[] data = null;    //集合的元素個數    private int size = 0;    //定義一個常量為 10.(後面用於定義預設的集合大小)    private static final int DEFAULT_CAPACITY = 10;         /***     * 有參建構函式     * 指定數組的大小     * @param length       */    public MyArrayList(int initialCapacity){        if(initialCapacity < 0){            throw new IllegalArgumentException("非法的集合初始容量值 Illegal Capacity: "+                    initialCapacity);        }else{            //執行個體化數組            this.data = new Object[initialCapacity];        }    }         /***     * 無參建構函式     * 指定數組的初始大小為 10     */    public MyArrayList(){        this(DEFAULT_CAPACITY);    }         /***     * 1、複製原數組,並擴容一倍     * 2、複製原數組,並擴容一倍,並在指定位置插入對象     * @param index     * @param obj     */    public void checkIncrease(int index,Object obj){        if(size >= data.length){            //執行個體化一個新數組            Object[] newData = new Object[size*2];                         if(index == -1 && obj == null){                System.arraycopy(data, 0, newData, 0, size);            }else{                //將要插入索引位置前面的對象 拷貝                System.arraycopy(data, index, newData, index+1, size-index);            }                         //將 newData 數組賦值給 data數組            data = newData;            newData = null;        }    }         /***     * 擷取數組的大小     * @return     */    public int getSize(){        return this.size;    }         /***     * 根據元素獲得在集合中的索引     * @param o     * @return     */    public int indexOf(Object o) {        if (o == null) {            for (int i = 0; i < data.length; i++)                if (data[i]==null)                    return i;        } else {            for (int i = 0; i < data.length; i++)                if (o.equals(data[i]))                    return i;        }        return -1;    }         /***     * 在尾部添加元素     * @param obj     * @return     */    public boolean add(Object obj){        //檢查是否需要擴容        checkIncrease(-1, null);        data[size++] = obj;        return true;             }         /**     * 判斷給定索引是否越界     * @param index     * @return     */    public boolean checkIndexOut(int index){        if(index > size || index < 0){            throw new IndexOutOfBoundsException("指定的索引越界,集合大小為:"+size+",您指定的索引大小為:"+index);        }        return true;    }         public boolean add(int index,Object obj){        //如果給定索引長度剛好等於原數組長度,那麼直接在尾部添加進去        if(index == size){            add(obj);        }        //checkIndexOut()如果不拋異常,預設 index <=size,且 index > 0        else if(checkIndexOut(index)){             if(size < data.length){                System.arraycopy(data, index, data, index+1, size-index);                data[index] = obj;            }else{                //需要擴容                checkIncrease(index, obj);            }            size++;        }                 return true;    }         /***     * 根據索引獲得元素     * @param index     * @return     */    public Object get(int index){        checkIndexOut(index);        return data[index];             }         /***     * 刪除所有元素     */    public void removeAll(){        for(int i = 0 ; i < data.length ; i++){            data[i] = null;        }    }         /***     * 根據索引刪除元素     * @param index     * @return     */    public Object remove(int index){        if(index == size+1){            throw new IndexOutOfBoundsException("指定的索引越界,集合大小為:"+size+",您指定的索引大小為:"+index);        }else if(checkIndexOut(index)){            //儲存對象            Object obj = data[index];            if(index == size){                data[index] = null;            }else{                //將後邊的數組向前移動一位                System.arraycopy(data, index+1, data, index, size-index);            }            size--;            return obj;        }                 return null;    }         /***     * 刪除指定的元素,刪除成功返回 true,失敗返回 false     * @param obj     * @return     */    public boolean remove(Object obj){        for(int i = 0 ; i < size ; i++){            if(obj.equals(data[i])){                remove(i);                return true;            }        }        return false;    }         /***     * 在指定位置修改元素,通過索引,修改完成後返回原資料     * @param index     * @param obj     * @return     */    public Object change(int index,Object obj){        checkIndexOut(index);        Object oldObj = data[index];        data[index] = obj;        return oldObj;    }         /***     * 查看集合中是否包含某個元素,如果有,返回 true,沒有返回 false     * @param obj     * @return     */    public boolean contain(Object obj){        for(int i = 0 ; i < data.length ; i++){            if(obj.equals(data[i])){                return true;            }        }        return false;    }                             public static void main(String [] args){                 MyArrayList my = new MyArrayList();        //System.out.println(my.data.length);        my.add(0,3);        //System.out.println(my.getSize());        my.add(0,4);        //System.out.println(my.getSize());        my.add(0,5);        //my.removeAll();        //my.remove(2);        //System.out.println(my.get(2));        System.out.println(my.indexOf(null));        System.out.println(my.contain(2));        for(int i = 0 ; i < my.data.length ; i++){            System.out.println(my.data[i]);        }                      }}

  


JAVA 用數組實現 ArrayList

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.