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

本文介绍了一个简单的自定义ArrayList类MyArrayList的实现过程,并通过MainTest类进行了一系列的功能测试,包括添加、删除、修改等操作,展示了动态数组的管理和使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码分为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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值