📜  如何在Java实现我们自己的动态数组类?

📅  最后修改于: 2021-09-07 02:59:40             🧑  作者: Mango

给定的任务是在Java实现一个类,它的行为就像使用 ArrayList 的动态数组一样。

ArrayList动态数组相同,具有在插入或删除元素时自动调整自身大小的能力,它们的存储由容器自动处理。

  • ArrayList 元素放置在连续存储中,以便可以使用迭代器访问和遍历它们。
  • 在 ArrayList 中,数据插入在最后。
  • 最后插入需要不同的时间,因为有时可能需要扩展阵列。
  • 删除最后一个元素只需要恒定的时间,因为不会发生大小调整。
  • 在开始或中间插入和擦除在时间上是线性的。

要在动态数组类中实现的函数:

我们将实现的与 ArrayList 相关的某些函数是:

  1. void push(int data) :该函数取一个元素并将其插入到最后一个元素。摊销时间复杂度为O(1)
  2. void push(int data, int index):在指定的索引处插入数据。时间复杂度是O(1)
  3. int get(int index):用于获取指定索引处的元素。时间复杂度是O(1)
  4. void pop():删除最后一个元素。时间复杂度是O(1)
  5. int size():它返回ArrayList 的大小,即ArrayList 中的元素数。时间复杂度是O(1)
  6. int getcapacity():它返回 ArrayList 的容量。时间复杂度是O(1)
  7. void print():用于打印数组元素。时间复杂度为O(N) ,其中 N 是 ArrayList 的大小。

动态数组类的实现:

下面是我们自己的 ArrayList 类的实现。

// Java program to implement
// our own Dynamic Array class
  
import java.util.*;
  
// Self implementation of
// the ArrayList Class in Java
class ArrayListClass {
  
    // arr is the array which stores
    // our ArrayList elements
    private int arr[];
  
    // capacity is the total storage
    // capacity of the ArrayList
    private int capacity;
  
    // current is the number of elements
    // currently present in the ArrayList
    private int current;
  
    // Default constructor to initialise
    // an initial capacity of 1 element and
    // allocating storage using dynamic allocation
    public ArrayListClass()
    {
        arr = new int[1];
        capacity = 1;
        current = 0;
    }
  
    // Function to add an element at the last
    public void push(int data)
    {
  
        // if the number of elements
        // is equal to the capacity,
        // that means we don't have space
        // to accommodate more elements.
        // We need to double the capacity
        if (current == capacity) {
            int temp[] = new int[2 * capacity];
  
            // copying old array elements
            // to new array
            for (int i = 0; i < capacity; i++)
                temp[i] = arr[i];
  
            capacity *= 2;
            arr = temp;
        }
  
        // Inserting data
        arr[current] = data;
        current++;
    }
  
    // function to add element at any index
    void push(int data, int index)
    {
  
        // if index is equal to capacity
        // then this function is same
        // as push defined above
        if (index == capacity)
            push(data);
        else
            arr[index] = data;
    }
  
    // Function to extract
    // element at any index
    int get(int index)
    {
  
        // if index is within the range
        if (index < current)
            return arr[index];
  
        // if index is outside the range
        return -1;
    }
  
    // function to delete last element
    void pop()
    {
        current--;
    }
  
    // function to get size
    // of the ArrayList
    int size()
    {
        return current;
    }
  
    // function to get capacity
    // of the ArrayList
    int getcapacity()
    {
        return capacity;
    }
  
    // function to print ArrayList elements
    void print()
    {
        for (int i = 0; i < current; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
  
    // Driver program to check ArrayListClass
    public static void main(String args[])
    {
        ArrayListClass v
            = new ArrayListClass();
        v.push(10);
        v.push(20);
        v.push(30);
        v.push(40);
        v.push(50);
  
        System.out.println("ArrayList size: "
                           + v.size());
        System.out.println(
            "ArrayList capacity: "
            + v.getcapacity());
        System.out.println(
            "ArrayList elements: ");
        v.print();
  
        v.push(100, 1);
  
        System.out.println(
            "\nAfter updating 1st index");
  
        System.out.println(
            "ArrayList elements: ");
        v.print();
        System.out.println(
            "Element at 1st index: "
            + v.get(1));
  
        v.pop();
  
        System.out.println(
            "\nAfter deleting the"
            + " last element");
  
        System.out.println(
            "ArrayList size: "
            + v.size());
        System.out.println(
            "ArrayList capacity: "
            + v.getcapacity());
  
        System.out.println(
            "ArrayList elements: ");
        v.print();
    }
}
输出:
ArrayList size: 5
ArrayList capacity: 8
ArrayList elements: 
10 20 30 40 50 

After updating 1st index
ArrayList elements: 
10 100 30 40 50 
Element at 1st index: 100

After deleting the last element
ArrayList size: 4
ArrayList capacity: 8
ArrayList elements: 
10 100 30 40