数据结构和算法------ArrayList的实现

概述:今年的秋招接近尾声了,在秋招的笔试和面试中,反映出了我自己技术栈的很多不足,最致命的地方在于数据结构和算法,计算机网络.在拿了三个Offer之后,结束秋招之旅.现在沉下心来,重新开始学习数据结构和算法,并且记录每一种数据结构的Java语言实现.同时也开始拜读[ThingKing in Java]和[Effective Java]这两本著作.争取在2017年的春招中拿到一份更加满意的Offer.


ArrayList的实现

public class MyArrayList<AnyType> implements Iterable<AnyType>{
    private static final int DEFAULT_CAPACITY=10;

    private int theSize;//ArrayList的长度
    private AnyType [] theItems;//内部使用数组实现

    public MyArrayList(){ //构造方法
        doClear();
    }

    public void clear(){
        doClear();
    }

    private void doClear(){
        theSize=0;//大小清零
        ensureCapacity(DEFAULT_CAPACITY);//容量改为默认大小
    }
    /*
        返回ArrayList大小
    */
    public int size(){
        return theSize;
    }
    /*
        判空
    */
    public boolean isEmpty(){
        return size()==0;
    }
    /*
        将容量大小设置为数组大小
    */
    public void trimToSize(){
        ensureCapacity(size());
    }
    /*
        查询
    */
    public AnyType get(int index){
        if (index<0||index>=size()) {
                throw new ArrayIndexOutOfBoundsException();
            }
        return theItems[index];
    }
    /*
        修改
        @return old 被修改位置处的原值
    */
    public AnyType set(int index,AnyType newVal){
        if (index<0||index>=size()) { //容错处理
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType old=theItems[index];
        theItems[index]=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;
    }
    /*
        添加元素到 index 位置处
    */
    public void add (int index,AnyType x){
        if (theItems.length==size()) {//当内部数组长度等于列表长时,就需要扩充容量
            ensureCapacity(size()*2+1); //此处+1是用于初始容量是0的情形
        }
        for (int i=theSize;i>index ;i--) {
            theItems[i]=theItems[i-1];//从后往前,将元素一个一个的后后移
        }
        theItems[index]=x;
        theSize++; //将列表的长度+1
    }
    /*
        删除
    */
    public AnyType remove(int index){
        AnyType removeItem=theItems[index];
        for (int i=index; i<size()-1;i++ ) {
            theItems[i]=theItems[i+1];
        }
        theSize--; //将列表的长度-1
        return removeItem; //返回被删除的元素值
    }
    /*
        实现Iterable接口的集合都必须提供一个名字为Iterator的方法,返回一个Iterator类型的对象
    */
    @Override
    public java.util.Iterator<AnyType> iterator(){
        return new ArrayListIterator();
    }
    /*

    */
    private class ArrayListIterator implements java.util.Iterator<AnyType>{
        private int current=0;

        public boolean hasNext(){
            return current<size();
        }
        public AnyType next(){
            if (!hasNext()) {
                throw new java.util.NoSuchElementException();
            }
            return theItems[current++];
        }
        public void remove(){
            //由于迭代器的remove()可能与MyArrayList的remove()冲突,因此我们必须使用MyArrayList.this.remove
            MyArrayList.this.remove(--current); 
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值