Skip to content
Related Articles

Related Articles

How to implement our own Dynamic Array class in Java?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 23 Apr, 2020

Given task is to implement a class in Java which behaves just like the Dynamic array using ArrayList.

ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

  • ArrayList elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
  • In ArrayList, data is inserted at the end.
  • Inserting at the end takes differential time, as sometimes there may be a need of extending the array.
  • Removing the last element takes only constant time because no resizing happens.
  • Inserting and erasing at the beginning or in the middle is linear in time.

Functions to be implemented in the Dynamic array class:

Certain functions associated with the ArrayList that we will implement are:

  1. void push(int data): This function takes one element and inserts it at the last. Amortized time complexity is O(1).
  2. void push(int data, int index): It inserts data at the specified index. Time complexity is O(1).
  3. int get(int index): It is used to get the element at the specified index. Time complexity is O(1).
  4. void pop(): It deletes the last element. Time complexity is O(1).
  5. int size(): It returns the size of the ArrayList i.e, number of elements in the ArrayList. Time complexity is O(1).
  6. int getcapacity(): It returns the capacity of the ArrayList. Time complexity is O(1).
  7. void print(): It is used to print array elements. Time complexity is O(N), where N is the size of the ArrayList.

Implementation of the Dynamic array class:

Below is the implementation of our own ArrayList class.

// 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;
    // 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)
            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()
    // 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] + " ");
    // Driver program to check ArrayListClass
    public static void main(String args[])
        ArrayListClass v
            = new ArrayListClass();
        System.out.println("ArrayList size: "
                           + v.size());
            "ArrayList capacity: "
            + v.getcapacity());
            "ArrayList elements: ");
        v.push(100, 1);
            "\nAfter updating 1st index");
            "ArrayList elements: ");
            "Element at 1st index: "
            + v.get(1));
            "\nAfter deleting the"
            + " last element");
            "ArrayList size: "
            + v.size());
            "ArrayList capacity: "
            + v.getcapacity());
            "ArrayList elements: ");


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!