- 将
ArrayIntList
转换为
ArrayList<E>
public ArrayIntList(int capacity){
if(capacity < 0)
throw new IllegalArgumentException("cpapcity: "+capacity);
elementData = new int[capacity];
size=0;
}
public ArrayIntList(E capacity){
if(capacity < 0)
throw new IllegalArgumentException("cpapcity: "+capacity);
elementData = new E[capacity];
size=0;
}
public ArrayIntList(E capacity){
if(capacity < 0)
throw new IllegalArgumentException("cpapcity: "+capacity);
elementData = (E())new object[capacity];
size=0;
}
public int indexof(E value){
for(int i=0;i<size;i++){
if(elementData[i]==value)
return i;
}
return -1;
}
public int indexof(E value){
for(int i=0;i<size;i++){
if(elementData[i]。equals(value))
return i;
}
return -1;
}
- 内部类
- 在类的内部声明一个类,内部的类的对象,可以访问外部类的方法和字段
-
ArryaListIterator<E>继承自Iterator<E>
private class ArrayListIterator implements Iterator<E> {
private int position;
private boolean removeOK;
public ArrayListIterator() {
position = 0;
removeOK = false;
}
public boolean hasNext() {
return position < size();
}
public E next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
E result = elementData[position];
position++;
removeOK = true;
return result;
}
public void remove() {
if (!removeOK) {
throw new IllegalStateException();
}
ArrayList.this.remove(position - 1);
position--;
removeOK = false;
}
}