Java数据结构之数组(三)

泛型动态数组

/**
 * 泛型数组
 * 
 * @author zyw
 */
public class GenericArray<E> {
	/**
	 * 数组使用int数组
	 */
	private E[] data;
	/**
	 * 数组的当前索引
	 */
	private int index;

	/**
	 * 数组初始容量大小为10
	 */
	public GenericArray() {
		this(10);
	}

	/**
	 * 构造方法可以自定义数组底层容量大小
	 * 
	 * @param capacity
	 */
	@SuppressWarnings("unchecked")
	public GenericArray(int capacity) {
		data = (E[]) new Object[capacity];
		index = 0;
	}

	/**
	 * @Desc 获取当前数组当前访问的索引
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日上午11:48:11
	 */
	public int getIndex() {
		return this.index;
	}

	/**
	 * @Desc 获取当前数组的数据个数
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日上午11:48:33
	 */
	public int getCapacity() {
		return this.data.length;
	}

	/**
	 * @Desc 向数组元素后添加一个元素
	 * @param e
	 * @Author zyw
	 * @Date 2019年3月3日上午11:49:47
	 */
	public void addLast(E e) {
		/*
		 * if (index == data.length) { throw new
		 * RuntimeException("数组添加失败,数组已满"); }
		 */
		add(index, e);
		/*
		 * data[index] = e; index++;
		 */
		// 以上两行也可以这样写:data[index++]=e;
		// 此方法可以调用add方法:add(index,e),在当前索引添加一个元素
	}

	/**
	 * @Desc 特定索引
	 * @param insertIndex
	 * @param e
	 * @Author zyw
	 * @Date 2019年3月3日上午11:55:15
	 */
	public void add(int insertIndex, E e) {

		if (insertIndex < 0 || insertIndex > index) {
			throw new RuntimeException("数组添加失败,插入索引必须在0到" + index + "之间");
		}
		if (index == data.length) {
			resize(2 * data.length);
		}

		for (int i = index - 1; i >= insertIndex; i--) {
			// 从后面开始每一个元素向后移动一位,知道到特定索引
			data[i + 1] = data[i];
		}
		data[insertIndex] = e;
		index++;
	}

	/**
	 * @Desc 数组容量不够进行动态扩容
	 * @param capacity
	 * @Author zyw
	 * @Date 2019年3月3日下午3:06:43
	 */
	@SuppressWarnings("unchecked")
	private void resize(int capacity) {
		E[] newData = (E[]) new Object[capacity];
		for (int i = 0; i < index; i++) {
			newData[i] = data[i];
		}
		data = newData;
	}

	/**
	 * @Desc 在数组最前面添加一个元素
	 * @Author zyw
	 * @Date 2019年3月3日下午12:02:51
	 */
	public void addFirst(E e) {
		add(0, e);
	}

	/**
	 * @Desc 获取指定索引处的元素
	 * @param index
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:10:43
	 */
	public E get(int index) {
		if (index < 0 || index >= this.index) {
			throw new RuntimeException("获取数组元素失败,获取索引必须在0到" + (this.index - 1) + "之间");
		}
		return data[index];
	}

	/**
	 * @Desc 获取第一个元素
	 * @return E
	 * @Author zyw
	 * @Date 2019年3月6日下午6:45:08
	 */
	public E getFirst() {
		return get(0);
	}

	/**
	 * @Desc 获取最后一个元素
	 * @return E
	 * @Author zyw
	 * @Date 2019年3月6日下午6:45:51
	 */
	public E getLast() {
		return get(index - 1);
	}

	/**
	 * @Desc 修改数组元素
	 * @param index
	 * @param e
	 *            void
	 * @Author zyw
	 * @Date 2019年3月3日下午12:15:25
	 */
	public void set(int index, E e) {
		if (index < 0 || index >= this.index) {
			throw new RuntimeException("修改数组元素失败,修改索引必须在0到" + (this.index - 1) + "之间");
		}
		data[index] = e;
	}

	/**
	 * @Desc 判断数组是否包含一个元素
	 * @param e
	 * @return boolean
	 * @Author zyw
	 * @Date 2019年3月3日下午12:17:35
	 */
	public boolean contains(E e) {
		for (int i = 0; i < index; i++) {
			if (data[i] == e)
				return true;
		}
		return false;
	}

	/**
	 * @Desc 获取指定元素的索引
	 * @param e
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:19:35
	 */
	public int find(E e) {
		for (int i = 0; i < index; i++) {
			if (data[i] == e)
				return i;
		}
		return -1;
	}

	/**
	 * @Desc 删除指定索引的元素,返回删除索引处的元素
	 * @param index
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:24:53
	 */
	public E remove(int index) {
		if (index < 0 || index >= this.index) {
			throw new RuntimeException("删除数组元素失败,删除索引必须在0到" + (this.index - 1) + "之间");
		}
		E res = data[index];
		for (int i = index + 1; i < this.index; i++) {
			// 特定索引的元素开始往前移动一位,直到数组结尾
			data[i - 1] = data[i];
		}
		this.index--;
		// 释放this.index处的空间,避免游荡对象
		data[this.index] = null;

		// 缩容操作
		if (this.index == data.length / 4 && data.length / 2 != 0) {
			resize(this.data.length / 2);
		}

		return res;
	}

	/**
	 * @Desc 删除最后一个元素
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:26:51
	 */
	public E removeLast() {
		return remove(index - 1);
	}

	/**
	 * @Desc 删除数组中第一个为e的元素,并返回索引
	 * @param e
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:33:02
	 */
	public int removeElement(E e) {
		int find = find(e);
		remove(find);
		return find;
	}

	/**
	 * @Desc 删除所有的元素e并返回元素的索引
	 * @param e
	 * @return MyArray
	 * @Author zyw
	 * @Date 2019年3月3日下午2:18:05
	 */
	public GenericArray<Integer> removeAllElement(E e) {
		GenericArray<Integer> myArray = new GenericArray<Integer>();
		for (int i = 0; i < index; i++) {
			if (data[i] == e) {
				remove(i);
				myArray.addLast(i);
			}
		}
		return myArray;
	}

	/**
	 * @Desc 删除最后一个数组元素
	 * @return int
	 * @Author zyw
	 * @Date 2019年3月3日下午12:27:41
	 */
	public E removeFirst() {
		return remove(0);
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append(String.format("Array:size = %d,capacity = %d\n", index, data.length));
		res.append("[");
		for (int i = 0; i < index; i++) {
			if (i != index - 1) {
				res.append(data[i]);
				res.append(",");
			} else {
				res.append(data[i]);
				res.append("]");
			}
		}
		return res.toString();
	}

}


猜你喜欢

转载自blog.csdn.net/weixin_40845956/article/details/88364988