MyarrayList自定义

自定义MyarrayList实现底层原理

接口
package myList;

public interface IList {
    // 新增元素,默认在数组最后新增
    public void add(int data) ;
    // 在 pos 位置新增元素
    public void add(int pos, int data) ;
    // 判定是否包含某个元素
    public boolean contains(int toFind) ;
    // 查找某个元素对应的位置
    public int indexOf(int toFind) ;
    // 获取 pos 位置的元素
    public int get(int pos) ;
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) ;
    //删除第一次出现的关键字key
    public void remove(int toRemove) ;
    // 获取顺序表长度
    public int size() ;
    // 清空顺序表
    public void clear();

    // 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
    public void display() ;
    boolean isFull();
    public boolean isEmpty();


}
属性
public class MyarrayList{
public int []elem;
private final INIT_LENGTH=10;//初始长度
public int usedSized;//初始长度为0
//属性







}
  1. 构造函数
  2. public MyarrayList()
    {
    this.elem=new int[INIT_LENGTH];//初始分配内存
    usedSized=0;//初始有效数据长度
    }

  3. 添加元素
  4.  public void add(int data) {
            checkAdd();
            this.array[usedSize++]=data;
    
    
    
        }
      
        @Override
        public void add(int pos, int data) {
            chechAddIndex(pos);
            checkAdd();
            for(int i=usedSize-1;i>=pos;i--)
            {
                array[i+1]=array[i];
    
            }
            array[pos]=data;
            usedSize++;
    
    
    
        }
        public  void chechAddIndex(int pos)
        {
            if(pos<0||pos>usedSize)
            {
               throw new IllegalIndexAddException("插入下标不合法:"+pos);//自定义异常
            }
        }
    

    //添加前首先要检查当前容量满了没有
  5.   public boolean checkCapcty()
        {
            return usedSize==this.array.length;//检查容量
        }
        public void checkAdd()//扩容
        {
            if(checkCapcty())
            {
                this.array= Arrays.copyOf(array,array.length*2);
    
            }
        }
    

  6. 获取元素
  7.  public int get(int pos) {
                indexCheck(pos);
                return array[pos];
    
        }

  8. 删除元素
  9. public void remove(int toRemove) {
            int index=indexOf(toRemove);
            if(index==-1)
            {
                return;
            }
            for(int i=index;i<usedSize-1;i++)
            {
                array[i]=array[i+1];
            }
            usedSize--;
    
    
    
    
        }

  10. 修改元素
  11.  public void set(int pos, int value) {
            indexCheck(pos);
            array[pos]=value;
    
        }

  12. 清空列表
  13.   public void clear() {
            usedSize=0;
    
        }

  14. 判断列表是否为空
  15.  public boolean isEmpty() {
            return this.usedSize==0;
        }

  16. 获取列表大小
  17.   public int size() {
            return usedSize;
        }

  18. 判断列表是否包含某个元素
  19.  public boolean contains(int toFind) {
    
            for(int i=0;i<usedSize;i++)
            {
                if(array[i]==toFind)
                {
                    return true;
                }
            }
            return false;
    
        }

  20. 获取元素索引值
  21.  public int indexOf(int toFind) {
            for (int i = 0; i < usedSize; i++) {
                if (array[i] == toFind) {
                    return i;
                }
            }
            return -1;
        }
    

  22. 打印列表元素
  23. public void display() {
           for(int i=0;i<usedSize;i++)
           {
               System.out.println(array[i]+" ");
           }
    
        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值