java的ArrayList的简单实现

ArrayList的实现的核心方法就是数组,数组在内存中的地址是连续的,所以支持快速访问的。如果你想用一种数据结构,既能快速访问,又能快速在末端添加数据(arraylist在末端添加数据没有数组的数据移动),那么ArrayList是一种理想的数据结构。下面展现简单实现ArrayList的代码,不足之处,请大家告诉我。


package swu.comput;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * 
 * @author shen-pc java简单ArrayList的实现
 * @param <T>
 */
public class ForLoop<T> implements Iterable<T> {

	// 存储每一个值的数组
	private T[] list;
	// 数组长度
	private int size;
	private static final int DEFAULT_CAPACITY = 10;

	public ForLoop() {
		clear();
	}

	/**
	 * 用于初始化一个长度为10的数组
	 */
	public void clear() {
		size = 0;
		ensureCapacity(DEFAULT_CAPACITY);
	}

	/**
	 * 返回表的长度
	 * 
	 * @return 返回表的长度,-1表示数组为空,没有初始化
	 */
	public int size() {
		if (null == list) {
			return -1;
		}
		return size;
	}

	/**
	 * 把数组中没有存储值的位置“截取掉”
	 */
	public void trimToSize() {
		ensureCapacity(size);
	}

	/**
	 * 
	 * @param pos
	 * @return 返回位于表pos位置的值
	 */
	public T get(int pos) {
		if (pos < 0 || pos > size) {
			throw new ArrayIndexOutOfBoundsException();
		}
		return list[pos];
	}

	/**
	 * 
	 * @param t
	 *            需要改变后的值
	 * @param pos
	 *            要改变值的位置
	 * @return 返回被改变的值
	 */
	public T set(T t, int pos) {
		// 判断位置是否有效
		if (pos < 0 || pos >= size) {
			throw new ArrayIndexOutOfBoundsException();
		}
		T old = list[pos];
		list[pos] = t;
		return old;
	}

	/**
	 * 扩大数组的长度
	 * 
	 * @param newCapacity
	 *            需要扩大表的长度
	 */
	public void ensureCapacity(int newCapacity) {
		if (size > newCapacity) {
			return;
		}
		T[] oldList = list;
		list = (T[]) new Object[newCapacity];
		for (int i = 0; i < size; i++) {
			list[i] = oldList[i];
		}
		// 显示置空,因为oldList一直指向没有扩充长度的值
		oldList = null;
	}

	public void add(T t) {
		add(t, size);
	}

	/**
	 * 在pos的位置添加值,以前pos处的值向后移
	 * 
	 * @param t
	 * @param pos
	 */
	public void add(T t, int pos) {
		if (pos < 0) {
			throw new ArrayIndexOutOfBoundsException();
		} else if (pos > size) {
			// 测试java的arraylist发现,这里会抛出这种异常
			throw new IndexOutOfBoundsException("Index: " + pos + ", " + "Size: " + size);
		}

		if (size == list.length) {
			// +1是为了list的size是1的情况出现
			ensureCapacity(list.length * 2 + 1);
		}
		// 把pos位置以及pos位置以后的值向后移动
		for (int i = size; i > pos; i--) {
			list[i] = list[i - 1];
		}
		list[pos] = t;
		size++;
	}

	/**
	 * 移除pos位置的值,并且所有后面的值向前移动
	 */
	public void remove(int pos) {
		if (pos < 0 || pos > size - 1) {
			throw new ArrayIndexOutOfBoundsException();
		}
		for (int i = pos; i < size - 1; i++) {
			list[i] = list[i + 1];
		}
		list[size - 1] = null;
		size--;
	}

	/**
	 * 内部类,和forloop对象关联在一起
	 * 
	 * @author shen-pc
	 *
	 * @param <T>
	 */
	private class MyIterator<T> implements Iterator<T> {

		private int currunt = 0;

		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			if (currunt < size) {
				return true;
			}
			return false;
		}

		@Override
		public T next() {
			// TODO Auto-generated method stub
			if (hasNext() == true) {
				return (T) list[currunt++];
			} else {
				throw new NoSuchElementException();
			}

		}

		@Override
		public void remove() {
			// TODO Auto-generated method stub
			// 调用外部内定义的remove方法
			ForLoop.this.remove(--currunt);
		}

	}

	/**
	 * 返回一个iterator对象
	 */
	@Override
	public Iterator<T> iterator() {
		// TODO Auto-generated method stub
		return new MyIterator<>();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值