博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ArrayList简单实现Java版
阅读量:4229 次
发布时间:2019-05-26

本文共 2364 字,大约阅读时间需要 7 分钟。

public class MyArrayList<AnyType> implements Iterable<AnyType>{
    private static final int DEFAULT_CAPACITY=10;//定义初始长度
    private int theSize;//当前位置
    private AnyType[] theItems;
    public MyArrayList(){
        doClear();
    }
    public void clear(){
        doClear();
    }
    private void doClear(){
        theSize=0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
    public int size(){
        return theSize;
    }
    public boolean isEmpty(){
        return size()==0;
    }
    public void trimToSize(){//设置数组长度刚好为当前所用长度
        ensureCapacity(size());
    }
    public AnyType get(int idx){//得到第idx+1个元素
        if(idx<0||idx>=size())
            throw new ArrayIndexOutOfBoundsException();
        return theItems[idx];
    }
    public AnyType set(int idx,AnyType newVal){//存值
        if(idx<0||idx>=size())
            throw new ArrayIndexOutOfBoundsException();
        AnyType old=theItems[idx];
        theItems[idx]=newVal;
        return old;
    }
    @SuppressWarnings("unchecked")
    public void ensureCapacity(int newCapacity){//重新分配存储单元
        if(newCapacity<theSize)
            return ;
        AnyType[] old=theItems;
        theItems=(AnyType[])new Object[newCapacity];//泛型数组的创建时非法的
        for(int i=0;i<size();i++)//重新赋值
            theItems[i]=old[i];
    }
    public boolean add(AnyType x){
        add(size(),x);
        return true;
    }
    public void add(int idx,AnyType x){
        if(theItems.length==size())
            ensureCapacity(size()*2+1);
        for(int i=theSize;i>idx;i--)
            theItems[i]=theItems[i-1];
        theItems[idx]=x;
        theSize++;
    }
    public AnyType remove(int idx){
        AnyType removedItem=theItems[idx];
        for(int i=idx;i<size()-1;i++)
            theItems[i]=theItems[i+1];
        theSize--;
        return removedItem;
    }
    @Override
    public Iterator<AnyType> iterator() {
        // TODO Auto-generated method stub
        return new ArrayListIterator();
    }
    //必须把ArrayListIterator申明为内部类,这样才可以访问MyArrayList的私有变量
    private class ArrayListIterator implements Iterator<AnyType>
    {
        private int current=0;
        @Override
        public boolean hasNext() {
            // TODO Auto-generated method stub
            return current<size();
        }
        @Override
        public AnyType next() {
            // TODO Auto-generated method stub
            if(!hasNext())
                throw new NoSuchElementException();
            return theItems[current++];
        }
        public void remove(){
            MyArrayList.this.remove(--current);
        }
    }

}

//测试

MyArrayList<Integer> a=new MyArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(1,4);
        for(Integer s:a){//测试迭代器
            System.out.print(s+",");
        }
        System.out.println();
        System.out.println(a.size()+","+a.get(1));
        a.remove(1);//删除
        Iterator<Integer> t=a.iterator();
        while(t.hasNext()){
            if(t.next()==1)
                t.remove();//用迭代器删除
        }
        for(Integer s:a){//测试迭代器
            System.out.print(s+",");
        }
    }

转载地址:http://lpjqi.baihongyu.com/

你可能感兴趣的文章
DeepMind发布最新《神经网络中持续学习》综述论文!
查看>>
本科三篇顶会一作、超算竞赛冠军,2020清华本科特奖结果出炉
查看>>
多语言互通:谷歌发布实体检索模型,涵盖超过100种语言和2000万个实体
查看>>
你的房东可能正用AI筛查你的犯罪记录,决定要不要租房给你
查看>>
AI把爱豆变胖视频火遍B站,我们找到了背后的技术团队:你是怎么把刘亦菲变胖的?...
查看>>
白硕:区块链技术与数据隐私(附视频)
查看>>
数据蒋堂 | 报表工具的SQL植入风险
查看>>
AAC ADTS LATM 格式分析
查看>>
【转载】嵌入式系统 Boot Loader 技术内幕
查看>>
【转载】uboot学习笔记
查看>>
分布式消息中间件(rabbitMQ篇)
查看>>
JAVA程序员养成计划之JVM学习笔记(2)-垃圾收集管理
查看>>
JAVA程序员养成计划之JVM学习笔记(3)-JVM性能监控
查看>>
POJ 3580
查看>>
POJ 2482
查看>>
POJ 3363
查看>>
[LeetCode] 849. Maximize Distance to Closest Person @ python
查看>>
axi总线介绍
查看>>
Linux内核中ioremap映射的透彻理解
查看>>
ffs的另外一种实现方法
查看>>