Java手写数组和案例拓展

Java手写数组和案例拓展

1. Mermanid代码绘制的思维导图解释实现思路原理

自定义数组类
数组容量
元素个数
元素存储空间
添加元素
获取元素
删除元素
查找元素
更新元素
扩容操作
检查容量
将元素添加到末尾
更新元素个数
返回指定位置的元素
查找指定元素的位置
删除指定位置的元素
将后面的元素向前移动
查找指定元素的位置
查找指定元素的位置
更新指定位置的元素
创建新的数组
将旧数组的元素复制到新数组
更新引用指向新数组

手写数组实现思路和原理

  1. Array类

    • 成员变量:

      • data:用于存储数组元素的整型数组
      • size:记录数组中元素的个数
    • 构造方法:

      • Array(int capacity):根据指定容量创建数组对象
    • 成员方法:

      • getSize():获取数组中元素的个数
      • getCapacity():获取数组的容量
      • isEmpty():判断数组是否为空
      • isFull():判断数组是否已满
      • get(int index):获取指定位置的元素
      • set(int index, int value):修改指定位置的元素
      • add(int value):向数组末尾添加元素
      • insert(int index, int value):在指定位置插入元素
      • remove(int index):删除指定位置的元素
      • indexOf(int value):查找指定元素的索引
      • contains(int value):判断数组是否包含指定元素
      • sort():对数组进行排序
  2. 主程序

    • 实例化Array对象
    • 调用Array对象的各种方法进行数组操作

2. 实现的详细介绍和详细步骤

2.1 定义一个Array类

首先,我们需要定义一个Array类,用于实现手写数组的各种操作。在Array类中,我们需要定义以下成员变量:

  • 数组容量capacity:表示数组的最大容量。
  • 当前元素个数size:表示数组中已经存储的元素个数。
  • 存储元素的数组data:用于存储实际的元素。

在Array类中,我们还需要定义以下构造方法:

public Array(int capacity) {
    this.capacity = capacity;
    this.size = 0;
    this.data = new int[capacity];
}

该构造方法用于初始化数组容量和当前元素个数。

2.2 成员方法的实现

在Array类中,我们需要实现各种成员方法,用于实现数组的各种操作。以下是各个方法的实现步骤:

  • 获取数组容量:
public int getCapacity() {
    return this.capacity;
}
  • 获取当前元素个数:
public int getSize() {
    return this.size;
}
  • 判断数组是否为空:
public boolean isEmpty() {
    return this.size == 0;
}
  • 判断数组是否已满:
public boolean isFull() {
    return this.size == this.capacity;
}
  • 获取指定位置的元素:
public int get(int index) {
    if (index < 0 || index >= this.size) {
        throw new IllegalArgumentException("Index is out of range.");
    }
    return this.data[index];
}
  • 修改指定位置的元素:
public void set(int index, int value) {
    if (index < 0 || index >= this.size) {
        throw new IllegalArgumentException("Index is out of range.");
    }
    this.data[index] = value;
}
  • 向数组末尾添加元素:
public void add(int value) {
    if (this.isFull()) {
        throw new IllegalStateException("Array is full.");
    }
    this.data[this.size] = value;
    this.size++;
}
  • 在指定位置插入元素:
public void insert(int index, int value) {
    if (index < 0 || index > this.size) {
        throw new IllegalArgumentException("Index is out of range.");
    }
    if (this.isFull()) {
        throw new IllegalStateException("Array is full.");
    }
    for (int i = this.size - 1; i >= index; i--) {
        this.data[i + 1] = this.data[i];
    }
    this.data[index] = value;
    this.size++;
}
  • 删除指定位置的元素:
public void remove(int index) {
    if (index < 0 || index >= this.size) {
        throw new IllegalArgumentException("Index is out of range.");
    }
    for (int i = index + 1; i < this.size; i++) {
        this.data[i - 1] = this.data[i];
    }
    this.size--;
}
  • 查找指定元素的索引:
public int indexOf(int value) {
    for (int i = 0; i < this.size; i++) {
        if (this.data[i] == value) {
            return i;
        }
    }
    return -1;
}
  • 判断数组是否包含指定元素:
public boolean contains(int value) {
    return this.indexOf(value) != -1;
}

3. 总结

通过以上的实现步骤,我们成功地实现了一个手写数组。在Array类中,我们定义了各种成员变量和成员方法,用于实现数组的各种操作。我们可以通过实例化Array对象,并调用其各种方法来进行数组的操作。

在实现过程中,我们需要注意数组的边界条件和错误处理,以确保代码的健壮性和可靠性。

4. 拓展案例:数组排序

作为一个案例拓展,我们可以实现一个数组排序的方法。以下是对步骤2中的Array类进行拓展,增加一个排序方法:

public void sort() {
    for (int i = 0; i < this.size - 1; i++) {
        for (int j = 0; j < this.size - i - 1; j++) {
            if (this.data[j] > this.data[j + 1]) {
                int temp = this.data[j];
                this.data[j] = this.data[j + 1];
                this.data[j + 1] = temp;
            }
        }
    }
}

该方法使用冒泡排序算法对数组进行升序排序。我们可以在实例化Array对象后,调用sort方法来对数组进行排序。

通过以上的拓展案例,我们进一步加深了对手写数组的理解,并且扩展了其功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

竹山全栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值