您的位置:首页 > 其它

ArrayList.class 阅读随笔

2011-01-21 17:33 896 查看
最近在修改社区游戏的麻将时,发现麻将的桌子号与玩家坐位号是存在内存中的,语句如下:

setPlayer(seatId, user_id);
playerlist.add(user_id);
追踪了一下:
由List<Integer> playerlist = new ArrayList<Integer>();语句定义。
于是乎拿ArrayList来开刀。

文档开头说明是这样的,Resizable-array implementation of the List interface;
可见ArrayList是实现了List接口的一个可调整数组大小的类;
往下看,ArrayList又继承了AbstractList类(AbstractList实现了List接口的一个抽象类);
List接口继承自Collection接口(Collection继承Iterable接口);
Iterable空空如也,只有这么一句:Iterator<T> iterator();
最后发现,只有interface Iterator<E>模版接口才是最终源头,但是它不像C++那样在源头做了实现,

它只是定义了接口,具体的实现则由...(往下看)

Iterator何意,大家知道的,“迭代器”;
它只声明了三个接口:
boolean hasNext();
E next();
void remove();
可见设计者是多么简练,但我不明白,为什么remove()都有了,怎么没有add()呢?

先不管这个,先来看看是哪个家伙继承了这个接口,由上面可知,是Collection接口。
看了一下Collection接口,它继承并声明了另外一些接口:
int size();
boolean isEmpty();
boolean contains(Object o); //判断容器中是否有o这个元素
Iterator<E> iterator();
@return an array containing all of the elements in this collection
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);//有add()了。
boolean remove(Object o);
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
太多接口了。继续...
到了List接口,好像对Collection接口中的函数有了初步的实现,看...
有了E get(int index);E set(int index, E element);接口
看这个 void add(int index, E element);与上面的boolean add(E e);明显是重载了,但是还是没有实

现,还有这个E remove(int index);

继续往上走,来到AbstractList抽象类,实现了AbstractList抽象类还继承AbstractCollection,从名

字可以大概明白了,AbstractCollection也是泛型编程层面上的。具体类型的实现由AbstractList实现

,所只就分析AbstractList吧。
public abstract int size(); 还是没实现
//下面这个终于实现了
public boolean isEmpty() {
return size() == 0;
}

继续看...
Iterator接口的三个函数都在这里实现了。
public boolean hasNext() {
return cursor != size();
}

public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}

public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}

看到这里,好像有点类型C++的指针实现,虽然java中没有指针,但实现思想上是一样的,通过cursor和

lastRet来指向数组中的元素。所以这些让我想起了Java本身就是从C++衍生出来的一种语言。

继续往上看,就到我们的应用类了ArrayList.class,经一个轮回,我们又回到这里了,真兴奋。
看一下它的构造函数,挺有意思,实现是也是Object[]数组。
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}

只从根接口再实现了remove()接口,其它两个则由AbstractList来实现。
public E remove(int index) {
RangeCheck(index);

modCount++;
E oldValue = (E) elementData[index];

int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc do its work

return oldValue;
}

先到此吧,感觉写得有点乱。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: