用数组实现ArrayList

本文分享了作者从参考大神代码开始,深入思考并独立完成ArrayList实现的过程,包括构造函数、扩容机制、元素操作等功能的详细解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考了大神的代码后,经过自己的思考,写出了自己的ArraList,为自己鼓掌!

package com.xy.Array;


public class ArrayList {
       private Object data[];  //用于存储数据
       private int size=0;     //列表中所添加的元素个数
       
       /*
        * 无参的构造函数,指定数组的初始大小
        * http://moyunyu.iteye.com/blog/1472081
        */
       public ArrayList(){
           this(10);
       }
       
       /*
        * 指定数组的大小
        */
       public ArrayList(int length) {
           //判断参数是否合法
           if(length<0)
           {
               System.out.println("数组的长度不能为小于0");
           }
           else{
               //实例化数组
               this.data=new Object[length];
           }
       }
       
       /*
        * 作用1: 扩容, 复制原数组 作用2: 扩容, 复制原数组并在指定索引插入对象, 仅当index==-1, o==null
        */
       
       public void checkCapacity(int index,Object o){
           if(size>=data.length){
               //实例化一个新数组
               Object newData[]=new Object[size*2];
               if(index==-1&&o==null)
               {
                   //将数组data的元素拷贝到数组newData中去
                   System.arraycopy(data,0,newData,0,size);
               }else{
                   // 将要插入索引位置前面的对象拷贝  
                   System.arraycopy(data,0,newData,0,index);
                    // 插入对象  
                   newData[index]=o;
                   // 将要插入索引位置后面的对象拷贝  
                   System.arraycopy(data,index,newData,index+1,size-index);
               }
               //将newData数组赋给data数组
               data=newData;
               newData=null;
           }        
       }
       
       /*
        * 获取表的大小
        */
       public int getSize(){
           return this.size;
       }
       
       /*
        * 在尾部插入对象
        */
       public boolean add(Object o){
           //检查是否需要扩容
           checkCapacity(-1,null);
           //添加元素到表末尾
           data[size++]=o;        
           return true;
       }
       
       /*
        * 检查给定的索引是否合法
        * 越界则抛出异常,正常返回true
        */
       
       public boolean rangCheck(int index){
           if(index>size||index<0){
              throw new IndexOutOfBoundsException("您指定的索引越界,列表大小为:"+size+",您指定的索引为:"+index);
           }
           return true;
       }
       /*
        * 在表的索引位置插入数据
        */
       public  void add(Object o,int index){
           //尾部直接插入
           if(index==size+1){
               add(o);
           }
           //检查索引值是否合法
           else if(rangCheck(index)){
               //如果不需要扩容
               if(size<data.length){
               System.arraycopy(data,index,data,index+1,size-index);
               data[index]=o;
               }else{
                //   需要扩容
                   checkCapacity(index,o);
               }
               //列表的长度加1
               size++;
           }        
       }
       
       /*
        * 删除指定位置的元素,并返回该元素
        *
        */
       public Object remove(int index){
           if(index==size+1){
               throw new IndexOutOfBoundsException("您指定的索引越界,列表大小为:"+size+",您指定的索引为:"+index);
           }
               else if(rangCheck(index)){
                   //保存对象
                   Object o=data[index];
                   if(index==size)
                   {
                       data[index]=null;
                   }else{
                    // 将后边的前移  
                       System.arraycopy(data,index+1,data,index,size-index-1);
                   }
                   //将列表的长度减一
                   size--;
                   //返回对象
                   return o;
                   
               }
           return null;
           }
       
       /*
        * 删除指定的对象
        */
       public boolean remove(Object o){
           for(int i=0;i<size;i++){
               if(o.equals(data[i])){
                   remove(i);
                   return true;
               }
           }
           //没有找到返回FALSE
           return  false;
       }
       
       /*
        * 删除所有的元素
        */
       
       public void removeAll(){
           for(int i=0;i<size;i++){
               data[i]=null;
           }
       }
       /*
        * 查找索引位子上的元素
        */
       public Object find(int index){
           rangCheck(index);
           return data[index];
       }
       
       /*
        * 是否包含某个对象
        */
       public boolean contain(Object o){
           for(int i=0;i<size;i++){
               if(data[i].equals(o)){
                   return true;
               }
           }
           return false;
       }
       
       /*
        * 修改指定位置上的元素
        */
       public Object change(int index,Object o){
          Object old;
          rangCheck(index);
          old=data[index];
          data[index]=o;
          return old;
        
       }
       
       /*
        * 打印数组中的元素
        */
       public void printArrayList(){
           for(int i=0;i<size;i++){
               if(i==0){
                   System.out.print("{"+data[i]);
               }else if(i>0&&i<size-1){
                   System.out.print(","+data[i]);
               }else {
                   System.out.print(","+data[i]+"}");
               }
           }
       }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值