ArrayList类的实现

来源:互联网 发布:java语法分析器 编辑:程序博客网 时间:2024/06/05 17:00

本次简单介绍ArrayList泛型类的实现,为避免与类库中的类相混,将类名定义为MyArrayList。下面概括需要实现的细节。

  1. MyArrayList将保持基础数组,数组的容量,以及存储在MyArrayList中的当前项数。

  2. MyArrayList将提供一种机制以改变基础数组的容量。通过获得一个新数组,将老数组拷贝到新数组中来改变数组的容量,允许虚拟机回收老数组。

  3. MyArrayList将提供get和set的实现。

  4. MyArrayList将提供基本的例程,如size,isEmpty和clear。它们是典型的单行程序,还提供remove,以及两个不同版本的add。如果数组的大小和容量相同,那么这两个add例程将增加容量。

  5. MyArrayList将提供一个实现Iterator接口的类。这个类将存储迭代序列中的下一项的下标,并提供next、hasNext和remove等方法的实现。MyArrayList的迭代器方法直接返回实现的Iterator接口的该类的新构造的实例。

package com.zs.structures.list.arraylist;import java.util.Iterator;import java.util.NoSuchElementException;/** * ArrayList类的实现 * @author ZS * * @param <AnyType> */public class MyArrayList<AnyType> implements Iterable<AnyType>{    private static final int DEFAULT_CAPACITY = 10;    private int theSize;    private AnyType[] theItems;    public MyArrayList() {        clear();    }    public void clear()    {        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 index)    {        if(index < 0 || index >= size())        {            throw new ArrayIndexOutOfBoundsException();        }        return theItems[index];    }    public AnyType set(int index, AnyType newVal)    {        if(index < 0 || index >= size())        {            throw new ArrayIndexOutOfBoundsException();        }        AnyType old = theItems[index];        theItems[index] = newVal;        return old;    }    public void ensureCapacity(int newCapacity)    {        if(newCapacity < size())        {            return;        }        AnyType[] old = theItems;        AnyType[] newItems= (AnyType[])new Object[newCapacity];        for (int i = 0; i < size(); i++)         {            newItems[i] = old[i];        }        theItems = newItems;    }    public boolean add(AnyType x)    {        add(size(), x);        return true;    }    public void add(int index, AnyType x)    {        if(theItems.length == size())        {            ensureCapacity(size() * 2 + 1);        }        for (int i = size(); i > index; i--)         {            theItems[i] = theItems[i - 1];        }        theItems[index] = x;        theSize++;    }    public AnyType remove(int index)    {        AnyType old = theItems[index];        for (int i = index; i < size() - 1; i++)         {            theItems[i] =  theItems[i + 1];        }        theSize --;        return old;    }    @Override    public Iterator<AnyType> iterator()     {        return new ArrayListIterator();    }    private class ArrayListIterator implements Iterator<AnyType>    {        private int current = 0;        @Override        public boolean hasNext()         {            return current < size();        }        @Override        public AnyType next()         {            if(!hasNext())            {                throw new NoSuchElementException();            }            return theItems[current++];        }        public void remove()        {            MyArrayList.this.remove(current-- );        }    }}
0 0
原创粉丝点击