2018-09-27模拟ArrayList底层实现、

2018-09-28  本文已影响0人  消失的七月

1.模拟ArrayList底层实现:

public class MyArrayList {

    StringBuilder s;//防照StringBuilder

    ArrayList a;//在查看Arraylist的一些简单方法

    /**

    *  The value is used for Object storage.

    */

    private Object[] value; 

    /**

    * The size is the number of Objects used.

    */

    private int size;

    public MyArrayList(){

        //value = new Object[16];

        this(10);

    }

    /**

    * Creates an MyArrayList of the specified capacity

    * 指定容量

    */

    public MyArrayList(int size){

        if(size < 0){

        try {

            throw new Exception();

        } catch (Exception e) {

            e.printStackTrace();

    }

}

    value = new Object[size];

}

    public void add(Object obj){

        value[size] = obj;

        size++;

        if(size >= value.length){

    //默认为10,大了的时候需要扩容

    int newCapacity = value.length * 2;

    Object[] newList = new Object[newCapacity];

    // System.arraycopy(src, srcPos, dest, destPos, length);

    for(int i = 0;i < value.length;i++){

        newList[i] = value[i];

    }

    value = newList;

    }

}

    public Object get(int index){

    if(index < 0 || index > size-1){

        try {

            throw new Exception();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    return value[index];

}

    public int size(){

        return size;

}

public boolean isEmpty() {

        return size == 0;

}

public boolean contains(Object o){

        return Indexof(o) >= 0;

}

private int Indexof(Object o) {

    if(o == null){

        for(int i = 0; i < size; i++)

            if(value[i] == null)

            return i;

    }else{

        for (int i = 0; i < size; i++)

                if (o.equals(value[i]))

                    return i;

    }

    return -1;

}

    //从头开始检索

    public int indexOf(Object o){

        if(o == null){

        for(int i = 0; i < size; i++){

            if(value[i]==null)

                return i;

    }

    }else{

        for(int i =0;i < size; i++){

            if(o.equals(value[i]))

                    return i;

    }

    }

    return -1;

}

public int lastIndexOf(Object o){

    if(o == null){

        for(int i = size - 1; i >= 0; i--){

            if(value[i]==null)

                return i;

    }

    }else {

        for(int i = size-1;i >= 0; i--){

            if(o.equals(value[i]))

            return i;

    }

}

    return -1;

}

public static void main(String[] args) {

    MyArrayList myList = new MyArrayList(2);//初始化为16,所以超过是应当去扩容

        myList.add("1");

        myList.add(2);

        myList.add(3);

        myList.add(2);

        myList.add(3);

        myList.add(2);

        myList.add(3);

        System.out.println(myList.get(0));//1

        System.out.println(myList.size);//7

        System.out.println(myList.isEmpty());//false

        System.out.println(myList.contains(null));//false

        System.out.println(myList.lastIndexOf(3));//6

    }

}

上一篇下一篇

猜你喜欢

热点阅读