Fork me on GitHub

ArrayList的底层实现

package zy809;

public class myArrayList {
	/** 存放元素 */
	private Object[] data;// 创建一个数组引用。

	/** 元素的个数 */
	private int size;// 一个指标,记录数组的元素个数。

	/*
	 * 三种构造方法。
	 */
	public myArrayList() {// 构造一个初始为10的空列表
		// data = new Object[10];
		this(10);
	}

	public myArrayList(myArrayList c) {
		//
		// data = c.data;
		data = new Object[c.size];

		for (int i = 0; i < c.size; i++) {
			data[i] = c.data[i];
		}

		size = c.size;
	}

	public myArrayList(int n) {// 指定容量
		if (n < 0) {
			throw new IllegalArgumentException();
		}
		data = new Object[n];
	}

	/*
	 * 待实现的方法
	 */
	public int size() {// 列表的元素数
		return size;
	}

	/** 可添加null元素 */
	public boolean add(Object o) {// 向滚动列表的末尾添加指定的项。
		this.kuoRongJianCe(size + 1);//先扩容数组
		data[size++] = o;
		return true;
	}

	public Object get(int index) {// 获取 指定位置的的
		yueJieJianCe(index, -1);//判断越界

		return data[index];
	}

	public void add(int index, Object o) {//指定位置 添加
		yueJieJianCe(index, 0);

		kuoRongJianCe(size + 1);

		for (int i = size - 1; i >= index; i--) {// 把index~size-1之间的元素后移
			data[i + 1] = data[i];
		}

		data[index] = o;
		size++;
	}

	public boolean addAll(myArrayList c) {

		this.kuoRongJianCe(size + c.size);

		for (int i = 0; i < c.size; i++) {
			data[size++] = c.data[i];
		}

		return true;
	}

	public boolean addAll(int index, myArrayList c) {
		this.yueJieJianCe(index, 0);
		this.kuoRongJianCe(size + c.size);
		// 从index位置开始腾挪c.size个空间
		int i = size - 1;
		for (; i >= index; i--) {// 把集合c中元素依此从index处插入
			data[i + c.size] = data[i];
		}
		// 拷贝c的元素至当前集合
		for (int j = 0; j < c.size; j++) {
			data[++i] = c.data[j];
		}

		size += c.size;

		return true;
	}

	public void clear() {
		// data = new Object[10];
		for (int i = 0; i < size; i++) {
			data[i] = null;
		}
		size = 0;
	}

	public boolean contains(Object o) {

		return this.indexOf(o) != -1;
	}

	/**
	 * 如有必要,增加此 MyArrayList 实例的容量,以确保它至少能够容纳最小容量参数所指定的元素数。 参数: minCapacity -
	 * 所需的最小容量
	 */

	public void ensureCapacity(int minCapacity) {
		int newsize = (int) (1.5 * size) + 1;
		if (minCapacity > newsize) {
			newsize = minCapacity;
		}
		Object[] temp = new Object[newsize];
		for (int i = 0; i < size; i++) {
			temp[i] = data[i];
		}
		data = temp;
	}

	public int indexOf(Object o) {// 返回此列表中首次出现的指定元素的索引,或如果此列表不包含元素,则返回 -1。

		return this.findElement(o, 0, 1);
	}

	/** 如果此列表中没有元素,则返回 true */
	public boolean isEmpty() {
		return size == 0;
	}

	public int lastIndexOf(Object o) {

		return this.findElement(o, size - 1, -1);
	}

	/** 移除指定位置元素,并返回该元素 */
	public Object remove(int index) {
		this.yueJieJianCe(index, -1);
		Object temp = data[index];
		//Object []arr=new Object[size-1];
		for (int i = index + 1; i < size; i++) {// 把index+1~size-1之间元素往前移
			data[i - 1] = data[i];
		}
        // data=arr;
		data[--size] = null;

		return temp;
	}

	public boolean remove(Object o) {
		int index;
		if ((index = this.indexOf(o)) != -1) {
			this.remove(index);

			return true;
		}

		return false;
	}
	/**
	 * 移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。向左移动所有后续 元素(减小其索引)。 此调用将列表缩短了
	 * (toIndex - fromIndex) 个元素。 (如果 toIndex==fromIndex,则此操作无效。)
	 */
	public void removeRange(int fromIndex, int toIndex) {
		if (fromIndex < 0 || fromIndex >= size || toIndex > size || fromIndex > toIndex) {
			throw new IndexOutOfBoundsException();
		}

		if (fromIndex == toIndex)
			return;

		int i = fromIndex;
		for (int j = toIndex; j < size; i++, j++) {
			data[i] = data[j];
		}

		for (; i < size; i++) {
			data[i] = null;
		}

		size -= toIndex - fromIndex;
	}

	/**
	 * 用指定的元素替代此列表中指定位置上的元素。 参数: index - 要替代的元素的索引 element - 存储在指定位置上的元素 返回:
	 * 以前位于该指定位置上的元素 抛出: IndexOutOfBoundsException - 如果索引超出范围 (index < 0 ||
	 * index >= size())
	 */
	public Object set(int index, Object element) {
		this.yueJieJianCe(index, -1);
		Object temp = data[index];
		data[index] = element;
		return temp;
	}

	/**
	 * 将此 ArrayList 实例的容量调整为列表的当前大小。应用程序可以使用此操作来 最小化 ArrayList 实例的存储量。
	 */
	public void trimToSize() {
		if (size < data.length) {
			Object temp[] = new Object[size];
			for (int i = 0; i < size; i++) {
				temp[i] = data[i];
			}
			data = temp;
		}
	}

	/** 如果列表包含指定 MyArrayList的所有元素,则返回 true */
	public boolean containsAll(myArrayList c) {
		if (c == null) {
			throw new NullPointerException();
		}
		int temp = c.size;
		for (int i = 0; i < temp; i++) {
			if (!contains(c.data[i])) {//不包含返回
				return false;
			}
		}
		return true;
	}

	/**
	 * 从列表中移除指定MyArrayList中包含的其所有元素 参数: c - 包含从此列表中移除的元素的 MyArrayList 返回:
	 * 如果此列表由于调用而发生更改,则返回 true
	 */
	public boolean removeAll(myArrayList c) {

		return jiaoji(c, 0);
	}

	/**
	 * 仅在列表中保留指定 MyArrayList中所包含的元素. 换句话说,该方法从列表中移除未包含在指定 MyArrayList中的所有元素。 参数:
	 * c - 包含将保留在此列表中的元素的 MyArrayList 返回: 如果此列表由于调用而发生更改,则返回 true
	 */
	public boolean retainAll(myArrayList c) {
		return jiaoji(c, 1);
	}

	/**
	 * 
	 * @param index
	 * @param pianYi
	 *            > size, 0; >= size, -1
	 */
	private void yueJieJianCe(int index, int pianYi) {//封装一个越界判定方法

		if (index < 0 || index > size + pianYi) {
			throw new IndexOutOfBoundsException("index = " + index+", size= "+size);
		}
	}

	private void kuoRongJianCe(int maxsize) {//封装传一个对象时的扩容方法
		if (maxsize > data.length) {
			this.ensureCapacity((int) (maxsize * 1.5) + 1);
		}
	}

	private int findElement(Object o, int qiShi, int buChang) {//封装有一个查找对象方法
		for (int i = qiShi; i >= 0 && i < size; i += buChang) {
			if ((o != null) ? o.equals(data[i]) : o == data[i]) {
				return i;
			}
		}

		return -1;
	}

	private boolean jiaoji(myArrayList o, int n) {//封装一个交集问题方法
		Object[] arr = new Object[size];
		int a1 = size;
		int j = 0;
		for (int i = 0; i < size; i++) {
			if ((n == 0) ? o.contains(data[i]) : !o.contains(data[i])) {
				arr[j++] = data[i];
			}
		}
		data = arr;
		size = j;
		/*
		 * else if (n == 1) { for (int i = 0; i < size; i++) { if
		 * (!o.contains(data[i])) { arr[j++] = data[i]; } } data = arr; size =
		 * j; }
		 */
		return a1 != size;
	}
}

  

posted @ 2018-08-10 18:49  雪人奥特曼  阅读(178)  评论(0编辑  收藏  举报