模仿ArrayList类的内部实现

JDK中的Arraylist类底层是数组存储对象,当数组满了的时候,会进行扩容,现在我们开始模仿ArrayList类,自己实现一个ArrayList类

模仿JDK中的ArrayList类,命名为:MyArrayList

成员

private static final int DEFAULT_CAPACITY=10;
private AnyType[] theItems;
private int theSize;

重要方法

MyArrayList.MyArrayList()
MyArrayList.ensureCapacity(int)//此方法,实现了扩容,紧缩,clear,构造。非常巧妙,读者可以多多分析,其他方法是如何使用ensureCapacity(int)方法的
MyArrayList.clear()
MyArrayList.size()
MyArrayList.isEmpty()
MyArrayList.get(int)
MyArrayList.set(int, AnyType)
MyArrayList.trimToSize()
MyArrayList.add(int, AnyType)
MyArrayList.add(AnyType)
MyArrayList.remove(int)
MyArrayList.iterator()

内部类(迭代器)

ArrayListIterator
current
hasNext()
next()
remove()
关于内部类(迭代器的作用),阅读这篇文章即可弄明白
http://blog.csdn.net/aa8568849/article/details/52638976
代码如下,读者应该能轻松读懂

public class MyArrayList<AnyType> implements Iterable<AnyType>
{
    private static final int DEFAULT_CAPACITY=10;
    private AnyType[] theItems;
    private int theSize;
    //--------------------------------构造器---------------------------
    public MyArrayList() {
        clear();
    }
    //------------------------------ensureCapacity-----------------------
    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];
    }
    //----------------------------------clear----------------------------
    public void clear()
    {
        theSize=0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
    //----------------------------------size----------------------------
    public int size(){
        return theSize;
    }
    //---------------------------------isEmpty-------------------------
    public boolean isEmpty()
    {
        return size()==0;
    }
    //-----------------------------------get---------------------------
    public AnyType get(int index)
    {
        if (index<0 || index>size()) 
            throw new ArrayIndexOutOfBoundsException();
        return theItems[index];
    }
    //-----------------------------------set---------------------------
    public AnyType set(int index,AnyType newVal)
    {
        if (index<0 ||index >size())
            throw new ArrayIndexOutOfBoundsException();
        AnyType old=theItems[index];
        theItems[index]=newVal  ;
        return old;
    }
    //--------------------------------trimToSize-----------------------
    public void trimToSize()
    {
        ensureCapacity(size());
    }
    //----------------------------add(index,obj)------------------------
    public void add(int index,AnyType x)
    {
        if (theItems.length==theSize) 
            ensureCapacity(theSize*2+1);
        for(int i=theSize;i>index;i--)
            theItems[i]=theItems[i-1];
        theItems[index]=x;
        theSize++;
    }
    //------------------------------add(obj)---------------------------
    public boolean add(AnyType x)
    {
        add(theSize, x);
        return true;
    }
    //------------------------------remove---------------------------
    public AnyType remove(int index)
    {
        if (index<0 || index>theSize-1) 
            throw new ArrayIndexOutOfBoundsException();
        AnyType removeItem=theItems[index];
        for(int i=index;i<theSize-1;i++)
            theItems[i]=theItems[i+1];
        theSize--;
        return removeItem;
    }
    @Override
    //覆写iterator方法,返回一个用于ArrayList的iterator对象
    public Iterator<AnyType> iterator() {
        // TODO Auto-generated method stub
        return new ArrayListIterator();
    }
    //内部类,实现arraylist的迭代器
    private class ArrayListIterator implements Iterator<AnyType>
    {
        private int current=0;
        public boolean hasNext()
        {
            return current<theSize;
        }
        public AnyType next()
        {
            if (!hasNext()) 
                throw new NoSuchElementException();
            return theItems[current++];
        }
        public void remove()
        {
            //使用next()获取当前对象后,current的值会+1,所以remove之前-1
            MyArrayList.this.remove(--current);

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值