手撸一个简单的arraylist。
顺便说下ConcurrentModificationException,出现这个异常的原因是创建迭代器时,迭代器会记录当前list操作的次数。modCount,当使用迭代器遍历时,而用了list的add或move,使得迭代器中的modCount和list中的modcount不一致,导致报错。
解决办法:要么不是用迭代器遍历,在集合中遍历。那就要注意遍历时因删除和增加引起的index变化。要么使用迭代器的remove方法,因为重写remove方法中,会重新给modcount赋值。
public class MyArrayList<AnyType> implements Iterable<AnyType> {
/**
* 三个变量:数组,数组长度,数组初始长度。
*
* 方法:
* 构造方法
* 增删改查
*
* 内部类:
* iterator
*
*
*
*/
private static final int NORMAL_SIZE = 10;
private int theSize;
private AnyType[] myItems;
private int modCount;
public MyArrayList(){
doInClear();
}
/**
* 功能:扩充数组容量
* @param count
*/
public void ensureCapacity(int count){
if(count < theSize){
return;
}
AnyType[] old = myItems;
myItems = (AnyType[]) new Object[count];
for (int i = 0;i<old.length;i++){
myItems[i] = old[i];
}
}
/**
* 功能:剪切掉空闲内存
*/
public void trimToSize(){
AnyType[] old = myItems;
myItems= (AnyType[]) new Object[theSize];
for (int i= 0;i<theSize;i++){
myItems[i] = old[i];
}
}
public void add(AnyType item){
add(theSize,item);
}
public void add(int index,AnyType item){
//判断index规范
if(index<0 ||index>theSize){
throw new IndexOutOfBoundsException();
}
//判断是否需要扩充数组
if(theSize >= myItems.length ){
ensureCapacity(theSize*2 +1);
}
//从后向前移动
for (int i = theSize;i>index;i--){
myItems[i] = myItems[i-1];
}
theSize++;
modCount++;
myItems[index] = item;
}
//直接删除最后一个 角标时theSize - 1
public AnyType remove(){
return remove(theSize -1);
}
//TODO -1 还是不减一
//不减一,最后一个就是null了
//减一最后一个还是存在的。
public AnyType remove(int index){
if(index<0||index>=theSize){
throw new IndexOutOfBoundsException();
}
AnyType removedItem = null;
removedItem = myItems[index];
//从前向后移
for (int i = index;i<theSize;i++){
myItems[i] = myItems[i+1];
}
theSize --;
modCount++;
return removedItem;
}
public int size(){
return theSize;
}
public void clear(){
doInClear();
}
public void doInClear(){
theSize = 0;
modCount = 0;
myItems = (AnyType[]) new Object[NORMAL_SIZE];
}
public String printList(){
StringBuffer stringBuffer = new StringBuffer("[");
for (int i = 0;i<theSize;i++){
stringBuffer.append(myItems[i]+",");
}
stringBuffer.append("]");
return stringBuffer.toString();
}
public AnyType set(int index,AnyType item){
AnyType oldItem = null;
if(index<0 ||index>= theSize){
throw new IndexOutOfBoundsException();
}
oldItem = myItems[index];
myItems[index] = item;
return oldItem;
}
public boolean isEmpty(){
return theSize == 0;
}
public AnyType get(int index){
if(index<0 ||index>= theSize){
throw new IndexOutOfBoundsException();
}
return myItems[index];
}
@NonNull
@Override
public Iterator<AnyType> iterator() {
return new MyListIterator();
}
private class MyListIterator implements Iterator<AnyType> {
public int myModCount = modCount;
public int index ;
public MyListIterator(){
index = 0;
}
@Override
public boolean hasNext() {
if(myModCount != modCount){
throw new ConcurrentModificationException();
}
return index<theSize;
}
@Override
public AnyType next() {
if(myModCount != modCount){
throw new ConcurrentModificationException();
}
return myItems[index++];
}
@Override
public void remove() {
if(myModCount != modCount){
throw new ConcurrentModificationException();
}
MyArrayList.this.remove(--index);
myModCount = modCount;
}
}
}
自己运行了下。。好像还可以。。明天撸linkedlist 复习下算法。