JAVA数据结构---动态扩展内存自定义ArrayList

代码分为2个部分:
向量类:MyArrayList.java
测试类:MainTest.java


向量类:MyArrayList.java

@SuppressWarnings("unchecked")
public class MyArrayList<T> {
    private T[] data;// 泛型数据
    private int count;// 加入的元素个数
    private final int DEFLAUT_SIZE = 10;// 默认的数组长度
    private int listSize;// 实例化数组的长度

    /**
     * 构造函数:有参数,无参数
     */

    public MyArrayList(int oldSize) {
        data = (T[]) (new Object[oldSize]);
        count = 0;
        listSize = oldSize;
    }

    public MyArrayList() {
        data = (T[]) (new Object[DEFLAUT_SIZE]);
        count = 0;
        listSize = DEFLAUT_SIZE;
    }

    /**
     * 私有的方法
     */
    private boolean isEnlarge() {
        boolean flag;
        if (isFull() == true) {
            T[] oldData = data;
            data = (T[]) (new Object[listSize + 1]);
            System.arraycopy(oldData, 0, data, 0, count);
            listSize++;
            flag = true;
        } else
            flag = false;
        return flag;
    }

    // 判断数组是否已经存满,这个是私有的方法。
    // 既然是动态的数组,对外肯定是不会满的,所以设置为私有的。
    private boolean isFull() {
        if (count == data.length)
            return true;
        else
            return false;
    }

    // 判断动态数组是否为空
    private boolean isEmpty() {
        if (count == 0)
            return true;
        else
            return false;
    }

    /**
     * 公开的方法
     */

    // 在指定位置添加
    public MyArrayList<T> add(int index, T t) {
        if (index > listSize) {
            throw new ArrayIndexOutOfBoundsException(index + ">" + listSize);
        }
        isEnlarge();// 检查是否需要扩充内存
        // 在中间位置插入时候需要将后面的元素后移
        if (index + 1 <= count) {
            for (int i = count - 1; i >= index; i--)
                data[i + 1] = data[i];
        }
        // 插入元素
        data[index] = t;
        count++;
        return this;
    }

    // 在尾部添加
    public MyArrayList<T> add(T t) {
        add(count, t);
        return this;
    }

    // 删除元素
    public MyArrayList<T> del(int index) throws Exception {
        if (isEmpty() == true)
            throw new Exception("数组为空无法删除...");
        else if (isEmpty() == false) {
            if (index <= count - 2) {
                for (int i = index; i <= count - 2; i++) {
                    data[i] = data[i + 1];
                }
                count--;
            } else if (index == count - 1) {
                count--;
            }
        }
        return this;
    }

    // 删除最后一个元素
    public MyArrayList<T> del() throws Exception {
        return del(count - 1);
    }

    // 修改
    public MyArrayList<T> set(int index, T t) {
        data[index] = t;
        return this;
    }

    // 查询
    public T get(int index) {
        if (index > count - 1 || index < 0) {
            System.out.println("下标错误...");
            return null;
        } else
            return data[index];
    }

    // 打印数组
    public void printList() {
        for (int i = 0; i < count; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
        System.out.println("长度是" + count);
    }

}

测试类:MainTest.java

public class MainTest {

    public static void main(String[] args) throws Exception {

        MyArrayList<String> mal = new MyArrayList<String>(3);
        mal.add("000").printList();
        mal.add(0, "111").printList();
        mal.add(1, "222").printList();
        mal.add("222").printList();
        mal.add("333").printList();
        mal.add("444").printList();
        mal.add("666").printList();
        mal.add(2, "YYY").printList();
        mal.del().printList();
        mal.del(2).printList();
        mal.del(0).printList();
        mal.add("999").printList();
        System.out.println(mal.get(1));
        mal.set(0, "9090").printList();
    }
}

测试结果:

000 
长度是1
111 000 
长度是2
111 222 000 
长度是3
111 222 000 222 
长度是4
111 222 000 222 333 
长度是5
111 222 000 222 333 444 
长度是6
111 222 000 222 333 444 666 
长度是7
111 222 YYY 000 222 333 444 666 
长度是8
111 222 YYY 000 222 333 444 
长度是7
111 222 000 222 333 444 
长度是6
222 000 222 333 444 
长度是5
222 000 222 333 444 999 
长度是6
000
9090 000 222 333 444 999 
长度是6
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章