学习完java集合容器.模仿一个简单的ArrayList.
模仿的简单的ArrayList支持增删查, 并且具有自动扩大容量的功能,增加遍历.
接口文件如下:- /**
- * <p>Title: ILinearList.java</p>
- * <p>Description: 线性表抽象接口</p>
- * <p>Copyright: Copyright (c) 2015</p>
- * @author possible
- * @version 1.0
- */
- package com.dt.linearlist;
- public interface ILinearList<T> {
- // /**
- // * 初始化线性表
- // *
- // * @return 返回线性表引用
- // */
- // public T initList();
- /**
- * 删除线性表
- *
- * @return 返回true 销毁成功 false失败
- */
- public void destoryList();
- /**
- * 判断线性表是否为空
- *
- * @return true 链表为空. false 链表不为空
- */
- public boolean isEmpty();
- /**
- * 线性表长度
- *
- * @return 链表长度
- */
- public int length();
- /**
- * 获取指定索引线性表结点
- *
- * @param index
- * 获取index位置的元素
- * @return 返回元素 T
- */
- public T get(int index);
- /**
- * 在指定位置插入元素
- *
- * @param i
- * 插入位置的索引
- * @param t
- * 插入的元素
- * @return true 插入成功, false 插入失败
- */
- public boolean add(int index, T t);
- /**
- * 删除指定索引的元素
- *
- * @param i
- * 删除指定位置的元素
- * @return true 删除成功; false 删除失败
- */
- public T delete(int index);
- /**
- * 线性表遍历
- *
- * @return 返回遍历结果
- */
- public String traverse();
- /**
- * 插入元素结点.
- *
- * @param t
- * 插入结点
- * @return true 插入成功 ; false 插入失败
- */
- public boolean add(T t);
- }
复制代码
对应模仿的简单ArrayList如下:
- /**
- * <p>Title: ArrayLinearList.java</p>
- * <p>Description:模仿ArrayList</p>
- * <p>Copyright: Copyright (c) 2015</p>
- * @author possible
- * @version 1.0
- */
- package com.dt.linearlist;
- import java.util.Arrays;
- /**
- * @author possible
- *
- */
- public class ArrayLinearList<T> implements ILinearList<T> {
- /**
- * 扩大容量的加载因子
- */
- private static final float LOADER_FACTOR = 0.8f;
- /**
- * 初始化容量
- */
- private static final int INITITAL_CAPACITY = 3;
- /**
- * 顺序表中真实存放的元素的个数
- */
- private int length;
- /**
- * 容量
- */
- private int capacity;
- private Object[] elements;
- public ArrayLinearList() {
- this(INITITAL_CAPACITY);
- }
- public ArrayLinearList(int inititalCapacity) {
- elements = new Object[inititalCapacity];
- this.capacity = inititalCapacity;
- }
- @Override
- public void destoryList() {
- for (int i = 0, len = elements.length; i < len; i++) {
- elements[i] = null;
- }
- length = 0;
- }
- @Override
- public boolean isEmpty() {
- return length == 0;
- }
- @Override
- public int length() {
- System.out.println("length = "+length+" capacity = "+capacity);
- return length;
- }
- @SuppressWarnings("unchecked")
- @Override
- public T get(int index) {
- if (!checkBounds(index)) {
- throw new ArrayIndexOutOfBoundsException(checkBoundsMsg(index));
- }
- return (T) elements[index];
- }
- @Override
- public boolean add(int index, T t) {
- if (!checkBounds(index)) {
- throw new ArrayIndexOutOfBoundsException(checkBoundsMsg(index));
- }
-
- if (isExtendCapacity()) {
- extendCapacity();
- }
- System.arraycopy(elements, index, elements, index+1, length-index+1);
- elements[index] = t;
- length ++;
- return true;
- }
- @Override
- public T delete(int index) {
- if (!checkBounds(index)) {
- throw new ArrayIndexOutOfBoundsException(checkBoundsMsg(index));
- }
- @SuppressWarnings("unchecked")
- T t = (T) elements[index];
- System.arraycopy(elements, index+1, elements, index, length-index-1);
- length--;
- return t;
- }
- @Override
- public String traverse() {
- StringBuffer sb = new StringBuffer();
- sb.append("开始遍历顺序表");
- sb.append("\n");
- for (int i = 0, len = length; i < len; i++) {
- if(elements[i] == null){
- break;
- }
- sb.append(elements[i].toString());
- sb.append("\n");
- }
- sb.append("结束遍历顺序表");
- System.out.println(sb.toString());
- return sb.toString();
- }
- @Override
- public boolean add(T t) {
- if (isExtendCapacity()) {
- extendCapacity();
- }
- elements[length++] = t;
- return true;
- }
- private String checkBoundsMsg(int index) {
- if (index >= length) {
- return "下标越界 " + " length: " + length + " index: " + index;
- }
- return null;
- }
- /**
- * 检查指定的索引是否越界
- *
- * @param index
- * @return
- */
- private boolean checkBounds(int index) {
- return index >= 0 && index < length;
- }
- /**
- * 判断是否需要扩大容量
- */
- private boolean isExtendCapacity() {
- if (length > capacity * LOADER_FACTOR) {
- return true;
- }
- return false;
- }
- /**
- * 扩展容量
- */
- private void extendCapacity() {
- System.out.println("扩大容量");
- int newLength = capacity * 2;
- elements = Arrays.copyOf(elements, newLength);
- capacity = newLength;
- }
- }
复制代码 |
|