Array Implementation in Java (Data Structure)

In this chapter, we will learn how to create and manage arrays in Java without using built-in classes. Writing your own array implementation helps you understand:

  • How elements are stored
  • How insertion and deletion really work
  • Why arrays have a fixed size
  • Why is shifting elements required
  • How actual data structures behave internally

This array code is an important step, specially for beginners, because it builds the foundation for learning Linked Lists, Stacks, Queues, and Dynamic Arrays later.

What This Code Covers

This Java code for array implementation includes:

  • Create an array with a fixed capacity
  • Insert at the end
  • Insert at any index (with shifting)
  • Delete element at index
  • Search value
  • Update value
  • Get element
  • Print array
  • Complete main() to test everything

Full Java Code — Array Implementation

// A simple Array implementation code in java.
// Supports insert, delete, search, update, and traversal.

class MyArray {
    int[] arr;     // internal array
    int size;      // current number of elements
    int capacity;  // total capacity

    // Constructor
    public MyArray(int capacity) {
        this.capacity = capacity;
        this.arr = new int[capacity];
        this.size = 0;
    }

    // Insert element at the end
    public void insert(int value) {
        if (size == capacity) {
            System.out.println("Array is full. Cannot insert " + value);
            return;
        }
        arr[size] = value;
        size++;
    }

    // Insert element at a specific index
    public void insertAt(int index, int value) {
        if (size == capacity) {
            System.out.println("Array is full. Cannot insert " + value);
            return;
        }
        if (index < 0 || index > size) {
            System.out.println("Invalid index");
            return;
        }
        // shift elements to the right
        for (int i = size - 1; i >= index; i--) {
            arr[i + 1] = arr[i];
        }
        arr[index] = value;
        size++;
    }

    // Delete element at a specific index
    public void deleteAt(int index) {
        if (size == 0) {
            System.out.println("Array is empty");
            return;
        }
        if (index < 0 || index >= size) {
            System.out.println("Invalid index");
            return;
        }

        // shift elements to the left
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }
        size--;
    }

    // Search for a value (linear search)
    public int search(int value) {
        for (int i = 0; i < size; i++) {
            if (arr[i] == value) {
                return i; // return index
            }
        }
        return -1; // not found
    }

    // Update value at index
    public void update(int index, int newValue) {
        if (index < 0 || index >= size) {
            System.out.println("Invalid index");
            return;
        }
        arr[index] = newValue;
    }

    // Get element at index
    public int get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Invalid index");
        }
        return arr[index];
    }

    // Print the array elements
    public void print() {
        if (size == 0) {
            System.out.println("Array is empty");
            return;
        }
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    // Returns current size
    public int getSize() {
        return size;
    }
}


// ============================
// Main Class to Test the Array
// ============================

public class ArrayImplementation {
    public static void main(String[] args) {

        MyArray myArr = new MyArray(10); // capacity = 10

        // Insert elements
        myArr.insert(10);
        myArr.insert(20);
        myArr.insert(30);
        myArr.insert(40);

        System.out.print("Array after insertion: ");
        myArr.print();  // 10 20 30 40

        // Insert at index
        myArr.insertAt(2, 25); // insert 25 at index 2
        System.out.print("Insert 25 at index 2: ");
        myArr.print();  // 10 20 25 30 40

        // Update element
        myArr.update(3, 35); // update index 3
        System.out.print("Update index 3 to 35: ");
        myArr.print();  // 10 20 25 35 40

        // Delete element
        myArr.deleteAt(1); // delete index 1
        System.out.print("Delete element at index 1: ");
        myArr.print();  // 10 25 35 40

        // Search element
        int index = myArr.search(35);
        if (index != -1)
            System.out.println("35 found at index: " + index);
        else
            System.out.println("35 not found");

        // Get element
        System.out.println("Element at index 2: " + myArr.get(2));  // 35

        // Final output
        System.out.print("Final Array: ");
        myArr.print();
    }
}