您的位置:首页 > 其它

17、实现一个简单的ArrayList类,以及静态嵌套类实现迭代器

2015-03-31 20:10 288 查看
太懒了没写注释

package Chapter3;

import java.util.*;
public class MyArrayList<T> implements Iterable<T>
{
private static final int DEFAULT_CAPACITY = 17;//默认大小
private int theSize;
private T[] items;

public MyArrayList()
{
clear();
}

public void clear()
{
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}

@SuppressWarnings("unchecked")
public void ensureCapacity(int newCapacity)
{
if(newCapacity <= theSize)
return;
T[] old = items;
items = (T[])new Object[newCapacity];
for(int i = 0; i < theSize; i++)
{
items[i] = old[i];
}
}

public int size()
{
return theSize;
}
public boolean isEmpty()
{
return (theSize == 0);
}

public T get(int index)
{
if(index < 0 || index >= theSize)
throw new ArrayIndexOutOfBoundsException();
return items[index];
}

public T set(int index, T newVal)
{
if(index < 0 || index >= theSize)
throw new ArrayIndexOutOfBoundsException();
T old = items[index];
items[index] = newVal;
return old;
}

public boolean add(T toadd)
{
add(theSize,toadd);
return true;
}

public void add(int index, T toadd)
{
if(theSize == items.length)
ensureCapacity(theSize * 2 - 1);
for(int i = theSize; i > index; i--)
{
items[i] = items[i - 1];
}
items[index] = toadd;
theSize++;
}

public T remove(int index)
{
T removed = items[index];
for(int i = index;i < theSize; i++)
{
items[i] = items[i + 1];
}
theSize--;
return removed;
}

@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new MyArrayListIterator();
}

private class MyArrayListIterator implements Iterator<T>//这里的类声明不用加泛型,内部类里的T要与所属外部类对象一致
{
private int current = 0;

@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return (current < size());
}

@Override
public T next() {
// TODO Auto-generated method stub
if(!hasNext())
throw new NoSuchElementException();
return items[current++];
}

public void remove()
{
MyArrayList.this.remove(--current);
}
}

}
测试代码如下,结果正常:

package Chapter3;

import java.util.*;

public class MyArrayListTest {
public static void main(String[] args)
{
MyArrayList<String> mALStr = new MyArrayList<String>();
for(int i = 0; i < 10; i++)
{
mALStr.add("String" + (i + 1));
}
Iterator<String> it = mALStr.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}

改用嵌套类实现迭代器

package Chapter3;

import java.util.*;
public class MyArrayList<T> implements Iterable<T>
{
private static final int DEFAULT_CAPACITY = 17;//默认大小
private int theSize;
private T[] items;

public MyArrayList()
{
clear();
}

public void clear()
{
theSize = 0;
ensureCapacity(DEFAULT_CAPACITY);
}

@SuppressWarnings("unchecked")
public void ensureCapacity(int newCapacity)
{
if(newCapacity <= theSize)
return;
T[] old = items;
items = (T[])new Object[newCapacity];
for(int i = 0; i < theSize; i++)
{
items[i] = old[i];
}
}

public int size()
{
return theSize;
}
public boolean isEmpty()
{
return (theSize == 0);
}

public T get(int index)
{
if(index < 0 || index >= theSize)
throw new ArrayIndexOutOfBoundsException();
return items[index];
}

public T set(int index, T newVal)
{
if(index < 0 || index >= theSize)
throw new ArrayIndexOutOfBoundsException();
T old = items[index];
items[index] = newVal;
return old;
}

public boolean add(T toadd)
{
add(theSize,toadd);
return true;
}

public void add(int index, T toadd)
{
if(theSize == items.length)
ensureCapacity(theSize * 2 - 1);
for(int i = theSize; i > index; i--)
{
items[i] = items[i - 1];
}
items[index] = toadd;
theSize++;
}

public T remove(int index)
{
T removed = items[index];
for(int i = index;i < theSize; i++)
{
items[i] = items[i + 1];
}
theSize--;
return removed;
}

@Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return new MyArrayListIterator<T>(this);
}
/**
* @author Administrator
*这里必须为MyArrayListIterator加上泛型参数
*是因为在它内部要以一个假设已知的泛型参数声明MyArrayList,
*这里不需要跟外部类的泛型参数T用相同的符号
*此处的泛型参数X用来在内部声明了MyArrayList<X> thislist,
*在外部类的iterator方法返回具体的迭代器对象时由T,
*也就是外部类的泛型参数作为形参传入,从而确定了X
* @param <X>
*/
private static class MyArrayListIterator<X> implements Iterator<X>
{
public MyArrayListIterator(MyArrayList<X> list)
{
thislist = list;
}
private int current = 0;
private MyArrayList<X> thislist;

@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return (current < thislist.size());
}

@Override
public X next() {
// TODO Auto-generated method stub
if(!hasNext())
throw new NoSuchElementException();
return thislist.items[current++];
}

public void remove()
{
thislist.remove(--current);
}
}
}

关于嵌套类:把一个类定义在另一个类内部,即叫嵌套类(nested class),而非静态嵌套类被成为内部类,李刚的书好像完全没讲这一点,真是跪了。。

用静态嵌套类实现迭代器的代码如上,因为静态嵌套类属于外部类本身,不属于外部类的具体对象,所以无法像内部类一样默认拥有一个对外部类对象的引用,所以需要在构造器中传入一个外部类实例的引用,而显式传入外部类实例则需要一个泛型参数,具体的要点看上面代码的文档注释部分。

假如使用内部类,当然可以同样的显式传入引用,但是没有那个必要。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐