模仿ArrayList类的内部实现,模仿arraylist类
分享于 点击 20703 次 点评:166
模仿ArrayList类的内部实现,模仿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);
}
}
}
相关文章
- 暂无相关文章
用户点评