在 Java 中创建动态数组
 
数组是一种固定大小的数据结构,其大小一旦声明就无法更改。动态数组为我们提供了创建动态大小数组的便利。我们可以相应地增加和减少这些大小,我们将在本文中讨论如何制作 Java 动态数组。
在 Java 中使用自定义逻辑创建动态数组
在示例中,我们使用自定义逻辑,其中包括在数组末尾或任何索引处添加元素的方法。当数组已满时,数组大小增加两倍。我们还在此过程中删除和缩小数组。
我们使用两个类来测试动态数组逻辑;第一个是 DynamicClass,第二个是 DynamicArrayTest 类。在 DynamicArrayTest 中,我们创建了一个 int 类型数组 intArray 和两个名为 size 和 capacity 的 int 变量。数组大小是其中的项目数,数组的容量是其中的总空间。
我们创建 DynamicArrayTest 类的构造函数,并使用大小为 2 的 int 数组初始化 intArray。然后,我们将 size 初始化为 0,将 capacity 初始化为 2。最后,为了在数组的最后位置添加一个元素,我们创建了 addElementToArray() 方法,该方法接受一个 int 元素作为参数。在这个函数中,我们首先检查数组的 size 和 capacity 是否相同。
如果为真,我们调用 increaseArraySize() 函数,因为数组已满。在 increaseArraySize() 方法中,我们创建了一个空的 int 数组 tempArray 来临时存储数组元素,并比较 size 和 capacity。我们用一个数组初始化 tempArray,并将其大小设置为数组当前容量的两倍。
在 increaseArraySize() 中,我们检查 capacity 是否大于或等于 0。然后我们调用 System.arraycopy() 方法将一个数组的元素复制到另一个数组。在那里,我们指定要复制的数组、要复制的起始索引、要复制元素的数组、目标位置以及我们想要的新数组的大小。毕竟,我们用 tempArray 的元素重新初始化 intArray 并增加 capacity 的大小。
现在,我们创建一个方法来删除元素并将其命名为 removeElement()。在这个函数中,我们检查数组的大小是否大于零。然后,我们用零替换数组的最后一个元素,并将大小减一。请注意,此方法仅删除数组的最后一个元素。
当数组已满时,该数组的容量会增加,并填充空白空间。这些空的、未使用的空间会增加内存使用和内存垃圾。为了解决这个问题,我们使用 shrinkSize() 函数删除空索引。在这里,我们创建一个临时数组并复制函数中 intArray 的所有元素,其大小与其元素相同,然后将数组元素复制回 intArray。
class DynamicArrayTest {
  int[] intArray;
  int size;
  int capacity;
  public DynamicArrayTest() {
    intArray = new int[2];
    size = 0;
    capacity = 2;
  }
  public void addElementToArray(int a) {
    if (size == capacity) {
      increaseArraySize();
    }
    intArray[size] = a;
    size++;
  }
  public void increaseArraySize() {
    int[] tempArray = null;
    if (size == capacity) {
      tempArray = new int[capacity * 2];
      {
        if (capacity >= 0) {
          System.arraycopy(intArray, 0, tempArray, 0, capacity);
        }
      }
    }
    intArray = tempArray;
    capacity = capacity * 2;
  }
  public void shrinkSize() {
    int[] temp;
    if (size > 0) {
      temp = new int[size];
      System.arraycopy(intArray, 0, temp, 0, size);
      capacity = size;
      intArray = temp;
    }
  }
  public void removeElement() {
    if (size > 0) {
      intArray[size - 1] = 0;
      size--;
    }
  }
}
public class DynamicArray {
  public static void main(String[] args) {
    DynamicArrayTest dynamicArrayTest = new DynamicArrayTest();
    dynamicArrayTest.addElementToArray(10);
    dynamicArrayTest.addElementToArray(20);
    dynamicArrayTest.addElementToArray(30);
    dynamicArrayTest.addElementToArray(40);
    dynamicArrayTest.addElementToArray(50);
    System.out.println("items of intArray:");
    for (int i = 0; i < dynamicArrayTest.capacity; i++) {
      System.out.print(dynamicArrayTest.intArray[i] + " ");
    }
    System.out.println();
    System.out.println("Capacity of the intArray: " + dynamicArrayTest.capacity);
    System.out.println("Size of the intArray: " + dynamicArrayTest.size);
    dynamicArrayTest.removeElement();
    System.out.println("\nItems after removing the last element");
    for (int i = 0; i < dynamicArrayTest.capacity; i++) {
      System.out.print(dynamicArrayTest.intArray[i] + " ");
    }
    System.out.println("\nCapacity of the intArray: " + dynamicArrayTest.capacity);
    System.out.println("Size of the intArray: " + dynamicArrayTest.size);
    dynamicArrayTest.shrinkSize();
    System.out.println("\nItems after removing unused space");
    for (int i = 0; i < dynamicArrayTest.capacity; i++) {
      System.out.print(dynamicArrayTest.intArray[i] + " ");
    }
    System.out.println("\nCapacity of the intArray: " + dynamicArrayTest.capacity);
    System.out.println("Size of the intArray: " + dynamicArrayTest.size);
  }
}
输出:
items of intArray:
10 20 30 40 50 0 0 0 
Capacity of the intArray: 8
Size of the intArray: 5
Items after removing the last element
10 20 30 40 0 0 0 0 
Capacity of the intArray: 8
Size of the intArray: 4
Items after removing unused space
10 20 30 
Capacity of the intArray: 3
Size of the intArray: 3
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
LinkedIn