ArrayList的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/starjingweij/article/details/80099380
import java.util.Iterator;

public class MyArrayList<AnyType> implements Iterable<AnyType>{
//    初始容量
    private static final int DEFAULT_CAPACITY=10;
//  当前容量
    private int theSize;
//    数组成员
    private AnyType[] theItems;
//  构造方法 用来初始化ArrayList
    public MyArrayList(){
        doClear();
    }
//  清空list
    public void clear(){
        doClear();
    }
//  清空list
    public void doClear(){
        theSize=0;
        ensureCapacity(DEFAULT_CAPACITY);
    }
//  获取listsize
    public int size(){
        return theSize;
    }
//  判断是否为空
    public boolean isEmpty(){
        return size()==0;
    }
//  整理list
    public void trimToSize(){
        ensureCapacity(size());
    }
//  获取某一项
    public AnyType get(int idx){
        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;
    }
//  扩充容量的方法
    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);
//        从最后一个元素的位置+1(也就是theSize)处循环到idx,i-1处的项赋值给i,最后把把x赋值给idx        for (int i = theSize;i>idx;i--)
            theItems[i] = theItems[i-1];
        theItems[idx] = x;
        theSize++;
    }
//    移除某一项
    public AnyType remove(int idx){
        AnyType removeItem = theItems[idx];
//        idx处到最右端,把每一项向左移动,最后theSize-1
        for (int i = idx; i < size()-1; i++)
            theItems[i] = theItems[i+1];
            theSize--;
            return  removeItem;

    }
//    实现了iterator,返回一个内部类ArrayListIterator
    @Override
    public Iterator<AnyType> iterator() {
        return new ArrayListIterator();
    }
    /*
    ArrayListIterator储存当前位置的概念,
    提供hasNext,next,remove的实现.当前位置表示要被查看的下一元素(的数组下标)
    所以初始时当前位置为0
    */    
    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(){
            MyArrayList.this.remove(--current);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/starjingweij/article/details/80099380