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

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

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,719评论 0 3
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,759评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,881评论 18 139
  • 作者:曾铁城 “坤坤,快点,我们到练功房去,快迟到了。” “爸爸,我的腿有点酸痛,下次再去练功吧。” “腿痛啊,爸...
    小城28阅读 343评论 0 2
  • Today is a sunshine day I have watched a nice movie<React...
    SeanWong阅读 291评论 0 0