Java_59_数组_模拟ArrayList容器的底层实现JDK源码分析代码

package cn.pmcse.myCollection;

public class ArrayList {
    private Object[] value;
    private    int count;//计数器 0
    ArrayList(){
//        value=new Object[16];
        this(16);//this 必须首行
    }
    ArrayList(int objsize) {
        value=new Object[objsize];//******************第二步 Object[] value=new Object[1]
    }
    public void copy(Object[] ar) {
        if(value.length<ar.length) {
            int newLength=ar.length*2;
            Object[] t2=new Object[newLength];
            value=t2;
        }
        for(int i=0;i<ar.length;i++) {
            value[i]=ar[i];
        }
    }
    public void add(Object obj) {
        value[count]=obj;
        System.out.println("********************"+count);//*************************** 0
        count++;
        System.out.println("********************"+count);//*************************** 1
        if(count>=value.length) {
            int newSize=value.length*2;//
            Object[] newList=new Object[newSize];
            for(int i=0;i<value.length;i++) {
                newList[i]=value[i];
            }
            value=newList;
        }
    }
    
    public Object get(int index) {
        if(index<0||index>value.length-1) {//0-(length-1)
            try {
                throw new Exception();//手动抛出异常。
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
        return value[index];//返回value索引index的对象
    }
    
    public void set(int index,Object obj) {
        if(index<0||index>value.length-1) {//0-(length-1)
            try {
                throw new Exception();//手动抛出异常。
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
        value[index]=obj;
    }
    public int size() {
        return count;//返回数量
    }
    public int indexOf(Object obj) {
        if(obj==null) {
            return -1;
        }else {
            for(int i=0;i<value.length;i++) {
                if(obj==value[i]) {
                    return 1;
                }
            }
            return -1;
        }
    }
    public int lastIndexOf(Object obj) {
        if(obj==null) {
            return -1;
        }else {
            for(int i=value.length-1;i>=0;i--) {
                if(obj==value[i]) {
                    return 1;
                }
            }
            return -1;
        }
    }
    public String toString() {
        return "默认调用这个玩意儿toString()";
    }
    public static void main(String[] args) {
        ArrayList list=new ArrayList(1); //******************第一步 size=1
        list.add(new Human("阿莫西林"));
        Human h=(Human)list.value[0];
        h.setName("胶囊");
        list.add("测试");
        ArrayList list2=new ArrayList(1);
        System.out.println(h.getName());//胶囊
        System.out.println(list.get(1));//测试
        System.out.println(list.count);//2
        System.out.println(list.size());//2
        System.out.println(list.value.length);//4
        System.out.println(list.indexOf(h));
        System.out.println(list.lastIndexOf(h));
        System.out.println(list.value[3]);
        list2.copy(list.value);
        Human h2= (Human)list2.value[0];
        list2.set(0, "对不对");
        System.out.println(list2.value[0]);
        System.out.println(list2.value.length);
        System.out.println(list2.count);
        System.out.println(h2.getName());
        
        
        ArrayList ss =new ArrayList();
        System.out.println(ss);
//        for(int i=0;i<16;i++) {
//            list.add(i);
//        }
//        System.out.println(list.value.length);
//        System.out.println(list.value[31]);
    }
}
 

 

word---------------------------------------------

 

import java.util.ArrayList;
 
public class MyArrayList {
 
       private Object value[];
       private int size;
 
       public MyArrayList() {
               // value = new Object[16];
               this(2);
       }
 
       public MyArrayList(int size) {
               value = new Object[size];
       }
 
       public void add(Object obj) {
               value[size] = obj;
               size++;
               if (size >= value.length) {
                        // 装不下了,将要扩展容量了
                        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 int size() {
               return size;
       }
 
       public boolean isEmpty() {
               return size == 0;
       }
 
       public int indexOf(Object obj) {
               if (obj == null) {
                        return -1;
               } else {
                        for (int i = 0; i < value.length; i++) {
                                 if (obj == value[i]) {
                                          return i;
                                 }
                        }
                        return -1;
               }
 
       }
 
       public int lastIndexOf(Object obj) {
               if (obj == null) {
                        return -1;
               } else {
                        for (int i = value.length - 1; i >= 0; i--) {
                                 if (obj == value[i]) {
                                          return i;
                                 }
                        }
                        return -1;// return 作用:1,返回出去值,2,结束语句。
               }
 
       }
 
       public void rangleCheck(int index) {
               if (index < 0 || index > size - 1) {// [0,size-1]
                        try {
                                 throw new Exception();
                        } catch (Exception e) {
                                 e.printStackTrace();
                        }
               }
       }
 
       public Object set(int index, Object object) {
               rangleCheck(index);
               Object old = value[index];
               value[index] = object;
               return old;
       }
 
       public Object get(int index) {
               rangleCheck(index);
               return value[index];
       }
 
       public static void main(String[] args) {
               MyArrayList list = new MyArrayList();
               list.add("aaa");
               list.add(new Human("高淇"));
               list.add("bbb");
               list.add("bbb");
               list.add("bbb");
               list.add("bbb");
               Human h = (Human) list.get(1);
               System.out.println(h.getName());
               System.out.println("*********************");
               System.out.println(list.get(0));
               System.out.println(list.get(1));
               System.out.println(list.get(2));
               System.out.println("*******************");
               System.out.println(list.size);
       
       }
 
}
 
class Human {
       private String name;
 
       public Human(String name) {
               this.name = name;
       }
 
       public String getName() {
               return name;
       }
 
       public void setName(String name) {
               this.name = name;
       }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_Pmc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值