数据结构 : Java实现动态数组

本文详细介绍如何使用Java语言实现动态数组,包括添加、获取、删除元素等操作,以及扩容和缩容策略,为初学者提供了一个完整的动态数组代码结构。

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

代码结构

import java.util.Objects;

public class Array<E> {
    private E[] data;
    private int size;

    //构造函数,传入数组的容量
    public Array(int capacity) {
        //强制转换capacity构造Array
        data = (E[]) new Object[capacity];  //data = new int[capacity];
        size = 0;
    }

    //无参数的构造函数,默认数组的容量capacity=10
    public Array() {
        this(10);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }
}

添加元素操作

在数组尾部添加元素只需要直接添加。
在数组头部或者中间其他位置插入时需要将该位置(包括该位置)以后的元素整体向后移。

 //在第index个位置插入一个新元素e
    public void add(int index, E e) {
        //如果index>size的话 说明数组不连续,不符合数组定义要求
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed.Require index >=0 and index<=size");
        }
		//动态实现扩容
        if (size == data.length) {
            resize(2 * data.length);
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = e;
        size++;
    }
    //在所有元素后添加一个新元素e
    public void addLast(E e) {
//        if(size == data.length) {
//            throw new IllegalArgumentException("AddLast failed.Array is full.");
//        }
//        data[size] = e;
//        size ++;
        //其实是add方法中的一种情况,可调用add()完成相关操作
        add(size, e);
    }

    //在所有元素前添加一个新元素
    public void addFirst(E e) {
        add(0, e);
    }

获取元素操作和判断元素是否存在操作

  // 获取index索引位置的元素
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed.Index is illegal.");
        return data[index];
    }
    //修改index索引位置的元素
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed.Index is illegal.");
        data[index] = e;
    }

    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e)) //这里使用的是equals() 因为用的是泛型,e代表的是对象
                return true;
        }
        return false;
    }

    //查找数组中元素e所在的索引,如果不存在元素e,则返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

删除操作

在数组尾部删除元素直接删除。
在数组头部或者中间其他位置删除元素时需要将该位置以后的元素整体向前移。

  // 从数组中删除index位置的元素,并且返回删除的元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed.Index is Illegal.");

        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];
        size--;
        data[size] = null; //loitering objects != memory leak
		//若设置为size == data.length / 2 时缩容可能会导致临界值加减时 性能大幅度降低
        if (size == data.length / 4 && data.length / 2 != 0)
         //data.length / 2 !=0 解决一个小bug:创建数组时不能创建空间为0的数组
            resize(data.length / 2);
        return ret;
    }

    // 从数组中删除第一个元素,返回删除的元素
    public E removeFirst() {
        return remove(0);
    }

    // 从数组中删除最后一个元素,返回删除的元素
    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

扩容操作

 //动态实现扩容
    private void resize(int newCapacity) {
        E[] newData = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++)
            newData[i] = data[i];
        data = newData;  //将地址传给原来数组
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值