Java Dynamic Arrays – Integers

Java Dynamic Arrays - Integers

The DynamicArrayOfInt class is a simple implementation of a dynamic array in Java. A dynamic array is a resizable array that can grow or shrink in size as needed. This class provides methods to manipulate and interact with this dynamic array of integers. In the constructor, initially the data array has a size of 1 and grows as necessary.

The method put(); is used to store the value at the specified position in the array. There is no pre-set limit on how large position can be, although for very large values there would be problems with having enough computer memory. The function get(); is used to retrieve the value stored in the specified position. If no value has ever been put at that position, then the value is zero.

Java Dynamic Array Example

import java.util.Arrays;

public class DynamicArrayOfInt {

    private int[] data; // An array to hold the data. The actual size
                         // of the array will increase as necessary.

    public DynamicArrayOfInt() {
        // Constructor. Create a new DynamicArrayOfInt object.
        // Initially, the data array only has size 1, but it will
        // grow as necessary.
        data = new int[1];
    }

    public int get(int position) {
        // Get the value from the specified position in the array.
        // Since all array positions are initially zero, when the
        // specified position lies outside the actual physical size
        // of the data array, a value of 0 is returned. However, the
        // array does NOT grow to include the specified position.
        if (position >= data.length)
            return 0;
        else
            return data[position];
    }

    public void put(int position, int value) {
        // Store the value in the specified position in the array.
        // The data array will increase in size to include this
        // position if necessary.
        if (position >= data.length) {
            // The specified position is outside the actual size of
            // the data array. Double the size, or if that still does
            // not include the specified position, set the new size
            // to 2*position. A new, larger array is created and
            // all the data from the old array is copied into it.
            // Then the instance variable, data, is set to refer
            // to the newly created array.
            int newSize = Math.max(2 * data.length, position + 1);
            data = Arrays.copyOf(data, newSize);
            // The following line is here for demonstration purposes only.
            System.out.println("Size of dynamic array increased to " + newSize);
        }
        data[position] = value;
    }

    public void remove(int position) {
        if (position >= 0 && position < data.length) {
            System.arraycopy(data, position + 1, data, position, data.length - position - 1);
            data = Arrays.copyOf(data, data.length - 1);
            System.out.println("Element at position " + position + " removed.");
        } else {
            System.out.println("Invalid position to remove.");
        }
    }

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

    public void clear() {
        data = new int[1];
        System.out.println("Dynamic array cleared. Size set to 1.");
    }

    public boolean contains(int value) {
        for (int element : data) {
            if (element == value) {
                return true;
            }
        }
        return false;
    }

    public void print() {
        System.out.println(Arrays.toString(data));
    }
}

Example Usage of Dynamic Array

// Create a dynamic array
DynamicArrayOfInt dynamicArray = new DynamicArrayOfInt();

// Add elements
dynamicArray.put(0, 10);
dynamicArray.put(1, 20);

// Print the array
dynamicArray.print(); // Output: [10, 20]

// Remove an element
dynamicArray.remove(0);

// Print the updated array
dynamicArray.print(); // Output: [20]

Class Members:

private int[] data: This is an array that holds the integer values. The size of this array increases dynamically when needed.

Constructor:

DynamicArrayOfInt(): This is the constructor of the class. It initializes the dynamic array with a default size of 1. As elements are added, the array will automatically resize to accommodate more elements.

Methods:

get(int position): int

  • Description: Retrieves the value at the specified position in the dynamic array.
  • Parameters: position: The index from which the value is to be retrieved.
  • Return Value: The integer value at the specified position. If the position is outside the current array bounds, 0 is returned.

put(int position, int value): void

  • Description: Stores a value at the specified position in the dynamic array. If the position is beyond the current array size, the array is resized to include the specified position.
  • Parameters:
  • position: The index at which the value is to be stored.
  • value: The integer value to be stored.

remove(int position): void

  • Description: Removes the element at the specified position in the dynamic array. The array is then resized to eliminate the gap.
  • Parameters: position: The index of the element to be removed.

size(): int

  • Description: Returns the current number of elements in the dynamic array.
  • Return Value: The number of elements in the array.

clear(): void

  • Description: Clears all elements from the array and sets its size back to 1.

contains(int value): boolean

  • Description: Checks if the specified value exists in the dynamic array.
  • Parameters: value: The value to be checked.
  • Return Value: true if the value is found, false otherwise.

print(): void

  • Description: Prints the elements of the dynamic array for debugging or testing purposes.

How to use the Dynamic Array?

  • Use the put method to add elements to the dynamic array.
  • Check the size of the array using the size method.
  • Remove elements using the remove method when necessary.
  • Clear the array using the clear method if you want to start fresh.
  • Use the contains method to check if a specific value is present in the array.
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post