15 内部类和ArrayList的实现

关于内部类:

写在内部就是私有的,内部类还是public纯属设计有问题,既然public了为嘛还要塞入内部,内部类一定要是public的话,初始化要依赖外部类的引用,不然类定义里面隐含的Outer.this无法实例化

Outer访问Inner: 一定要创建inner的对象(不然实例化的时候很明显有问题),要么就都是static;Inner在Outer中实例化以后,可以直接Inner的私有属性,这个是强制规定,当然有点破坏了automic unit的规定,嵌套类也是如此

Inner访问Outer:默认有个Outer.this引用,当然Outer的属性和方法在Inner内可以直接访问,不需加Outer.this区分(名字没有二义性的时候,不然会被覆盖)




关于ArrayList的设计:

迭代器remove()更好,当然这个方法过于依赖其他方法的实现,耦合很大,比如内部的字段expectedModCount因为要知道ArrayList的modCount会修改才修改。

ConcurrentModificationException通过字段modCount实现,当然没有同步,所以并发的时候还是会有错不提示,只是一个思路罢了。


代码:

package nuaa.ds;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;

public class ArrayList<T> extends AbstractCollection<T> implements List<T> {

	private static final int DEFAULT_CAPACITY = 2;
	private static final int NOT_FOUND = -1;
	
	private T[] theItems;
	private int theSize;
	private int modCount = 0;//保存对对象的修改次数,目的是保持迭代器和集合类的一致性
	
	public ArrayList(){
		clear();
	}
	
	public ArrayList(Collection<? extends T> c){
		clear();
		for(T obj:c)
			add(obj);
	}
	
	
	@Override
	public void clear() {
		theSize = 0;
		theItems = (T[])new Object[this.DEFAULT_CAPACITY];
		modCount++;
	}

	@Override
	public boolean addAll(int index, Collection<? extends T> c) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public T get(int index) {
		if(index<0||index>=size())
			throw new ArrayIndexOutOfBoundsException();
		return theItems[index];
	}

	@Override
	public T set(int index, T element) {
		if(index<0||index>size())
			throw new ArrayIndexOutOfBoundsException();
		T old = theItems[index];
		theItems[index] = element;
		return old;
	}
	
	
	@Override
	public boolean contains(Object o) {
		return findPos(o)!=NOT_FOUND;
	}
	
	private int findPos(Object o){
		for(int i=0;i<size();i++){
			if(o==null){
				if(theItems[i]==null)
					return i;
			}else if(o.equals(theItems[i])){
				return i;
			}
		}
		return this.NOT_FOUND;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean add(T e) {
		if(theItems.length==size()){
			T[] old = theItems;
			theItems = (T[])new Object[theItems.length*2+1];
			for(int i=0;i<size();i++)
				theItems[i] = old[i];
		}
		theItems[theSize++] = e;
		modCount++;
		return true;
	}

	@Override
	public void add(int index, T element) {
		
	}

	@Override
	public T remove(int index) {
		T removedItem = theItems[index];
		for(int i=index;i<size()-1;i++)
			theItems[i] = theItems[i+1];
		theSize--;
		modCount++;
		return removedItem;
	}
	
	@Override
	public boolean remove(Object o) {
		int pos = findPos(o);
		if(pos==this.NOT_FOUND)
			return false;
		else{
			remove(pos);
			return true;
		}
	}

	@Override
	public int indexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int lastIndexOf(Object o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ListIterator<T> listIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ListIterator<T> listIterator(int index) {
		return new ArrayListIterator(index);
	}

	@Override
	public List<T> subList(int fromIndex, int toIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Iterator<T> iterator() {
		return new ArrayListIterator(0);
	}

	@Override
	public int size() {
		return theSize;
	}
	private class ArrayListIterator implements ListIterator<T>{

		private int current;
		private int expectedModCount = modCount;
		private boolean nextCompleted = false;
		private boolean prevCompleted = false;
		
		ArrayListIterator(int pos){
			if(pos<0||pos>size())
				throw new IndexOutOfBoundsException();
			current = pos;
		}
		@Override
		public boolean hasNext() {
			if(expectedModCount!=modCount)
				throw new ConcurrentModificationException();
			return current<size();
		}

		@Override
		public T next() {
			if(!hasNext())
				throw new NoSuchElementException();
			nextCompleted = true;
			prevCompleted = false;
			return theItems[current++];
		}

		@Override
		public boolean hasPrevious() {
			if(expectedModCount!=modCount)
				throw new ConcurrentModificationException();
			return current>0;
		}

		@Override
		public T previous() {
			if(!hasPrevious()){
				throw new NoSuchElementException();
			}
			prevCompleted = true;
			nextCompleted = false;
			return theItems[--current];
		}

		@Override
		public int nextIndex() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public int previousIndex() {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		/**
		 * 迭代器的remove因为已经有了current,
		 * 因此删除的时候很明显比类原始的remove(obj)方法少一个阶的复杂度
		 */
		public void remove() {
			if(expectedModCount!=modCount)
				throw new ConcurrentModificationException();
			if(nextCompleted)
				ArrayList.this.remove(--current);//调用next的时候current已经指向下一项
			else if(prevCompleted)
				ArrayList.this.remove(current);
			else
				throw new IllegalStateException();
			prevCompleted = nextCompleted = false;
			expectedModCount++;//调用容器的remove方法属性modCount会自增
		}

		@Override
		public void set(T e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void add(T e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	//cheap main
	public static void main(String[] args){
		ArrayList<Integer> al = new ArrayList<Integer>();
		for(int i=0;i<20;i++){
			al.add(i);
		}
		System.out.print(al.toString());
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值