数据结构--ArrayList的Java实现

来源:互联网 发布:淘宝女裤品牌 编辑:程序博客网 时间:2024/06/05 00:04

上代码:

package com.itany.MyArrayList;import java.util.Iterator;public class MyArrayList<T> implements Iterable<T>{    private static final int DEFAULT_CAPACITY=10;//默认数组容量大小    private int theSize;//集合中元素的个数大小    private T[] theItems;//集合中的数组    public MyArrayList()    {        theSize=0;        clear();    }    public int size()    {        return theSize;    }    //清除所有集合中的所有元素    public void clear()    {        theSize=0;        ensureCapacity(DEFAULT_CAPACITY);    }    public boolean isEmpty()    {        return size()==0;    }    //缩小数组大小 缩小到和集合中元素个数一样大    public void trimToSize()    {        ensureCapacity(size());    }    public T get(int idx)    {        //注意这里idx>最好写size()因为 get方法返回的是T 如果写theItems.length可能返回的是一个空 而不是对应的T        if(idx<0 || idx>=size())            throw new ArrayIndexOutOfBoundsException();        return theItems[idx];    }    //返回被修改的原始值    public T set(int idx,T newT)    {        if(idx<0 || idx>size())            throw new ArrayIndexOutOfBoundsException();        T old=theItems[idx];        theItems[idx]=newT;        return old;    }    //根据新的容量大小来扩充theItems数组的大小 并且把旧的数组内容传进新theItems的里面    public void ensureCapacity(int newCapacity)    {        //如果新容量大小<集合中元素的个数 则直接结束方法        if(newCapacity<theSize)            return;        T[] old=theItems;        //theItems=new T[newCapacity]; Cannot create a generic array of T不能直接new泛型数组        theItems=(T[])new Object[newCapacity];//只能这样写 这样的警告不可避免        //把所有旧的数组里面内容全部给到新的容量数组里面  注意在clear调用时 由于theSize=0 不会赋值        for(int i=0;i<size();i++)        {            theItems[i]=old[i];//剩余的为空        }    }    //直接默认加在最后一个    public void add(T newT)    {        add(size(),newT);    }    public void add(int idx,T newT)    {        //在插入时,需要考虑集合的元素个数是否已经等于数组的容量大小 负责需要对数组容量进行扩容        if(size()==theItems.length)            ensureCapacity(2*size()+1);        //此时数组容量是大于集合元素个数        //对于插入有两种可能 1如果插入位置>=size()大小的位置 那么不需要移动  2另一种 如果插入位置<size() 则需要平移        //但是两者都需要相同的语句theItems[idx]=newT        for(int i=size();i>idx;i--)//size()>idx的情况 不满足的话 不执行跳过即可        {            theItems[i]=theItems[i-1];        }        theItems[idx]=newT;//所有情况都要        theSize++;    }    public T remove(int idx)    {        T oldT=theItems[idx];        for(int i=idx;i<size()-1;i++)        {            theItems[i]=theItems[i+1];        }        theSize--;        return oldT;    }        @Override    //重写iterator方法 以获得一个自己写的ArrayListIterator迭代器    public Iterator<T> iterator()    {        // TODO Auto-generated method stub        return new ArrayListIterator();    }    //自己写的迭代器类ArrayListIterator    private class ArrayListIterator implements Iterator<T>    {        private int current=0;        @Override        public boolean hasNext()        {            return current<size();        }        @Override        public T next()        {            if(!hasNext())                throw new java.util.NoSuchElementException();            return theItems[current++];//current是先调用后+1        }        @Override        public void remove()        {            //调用完next之后 current已经+1 因此要先-1 再删除当前            //不能只写this.remove(--current); 因为这个this是ArrayListIterator对象            MyArrayList.this.remove(--current);        }    }}

package com.itany.MyArrayList;import java.util.Iterator;public class Test{        public static void main(String[] args)    {        MyArrayList<Integer> my=new MyArrayList<Integer>();        my.add(12);        my.add(11);        my.add(13);        my.add(2,14);        my.remove(1);        Iterator<Integer> it=my.iterator();        while(it.hasNext())        {            System.out.print(it.next()+"  ");        }    }    }


0 0
原创粉丝点击