Launch your tech mastery with us—your coding journey starts now!
Course Content
Data Structure

Array Implementation

Here is a complete Java program demonstrating Insertion and Deletion logic manually. Note that Java’s built-in arrays are fixed, so we simulate insertion/deletion by shifting elements.

JavaPythonCC++

import java.util.Scanner;

public class ArrayOperations {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // 1. Creation and Input
        int capacity = 100; // Maximum size buffer
        int[] arr = new int[capacity];
        int n = 0; // Current number of elements

        System.out.print("How many elements initially? ");
        n = sc.nextInt();

        System.out.println("Enter " + n + " elements:");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        // Display Initial Array
        System.out.println("Initial Array:");
        displayArray(arr, n);

        // 2. INSERTION Operation
        // Logic: Shift elements to the right
        System.out.print("\nEnter position to insert (0 to " + n + "): ");
        int pos = sc.nextInt();

        System.out.print("Enter value to insert: ");
        int val = sc.nextInt();

        if (pos >= 0 && pos <= n && n < capacity) {

            for (int i = n; i > pos; i--) {
                arr[i] = arr[i - 1];
            }

            arr[pos] = val;
            n++;

            System.out.println("Array after Insertion:");
            displayArray(arr, n);

        } else {
            System.out.println("Invalid Position!");
        }

        // 3. DELETION Operation
        // Logic: Shift elements to the left to fill the gap
        System.out.print("\nEnter position to delete (0 to " + (n - 1) + "): ");
        int delPos = sc.nextInt();

        if (delPos >= 0 && delPos < n) {

            for (int i = delPos; i < n - 1; i++) {
                arr[i] = arr[i + 1];
            }

            n--;

            System.out.println("Array after Deletion:");
            displayArray(arr, n);

        } else {
            System.out.println("Invalid Position!");
        }

        sc.close();
    }

    // Helper method to print array
    public static void displayArray(int[] arr, int size) {
        System.out.print("[ ");
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("]");
    }
}

# Array Operations

# 1. Creation and Input
capacity = 100
arr = [0] * capacity
n = 0

n = int(input("How many elements initially? "))

print("Enter", n, "elements:")
for i in range(n):
    arr[i] = int(input())

# Display Initial Array
print("Initial Array:")
display = arr[:n]
print("[", *display, "]")

# 2. INSERTION Operation
pos = int(input("\nEnter position to insert (0 to " + str(n) + "): "))
val = int(input("Enter value to insert: "))

if pos >= 0 and pos <= n and n < capacity:

    for i in range(n, pos, -1):
        arr[i] = arr[i - 1]

    arr[pos] = val
    n += 1

    print("Array after Insertion:")
    print("[", *arr[:n], "]")

else:
    print("Invalid Position!")

# 3. DELETION Operation
delPos = int(input("\nEnter position to delete (0 to " + str(n - 1) + "): "))

if delPos >= 0 and delPos < n:

    for i in range(delPos, n - 1):
        arr[i] = arr[i + 1]

    n -= 1

    print("Array after Deletion:")
    print("[", *arr[:n], "]")

else:
    print("Invalid Position!")

#include <stdio.h>

void displayArray(int arr[], int size) {
    printf("[ ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("]\n");
}

int main() {

    int capacity = 100;
    int arr[100];
    int n = 0;

    printf("How many elements initially? ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Initial Array:\n");
    displayArray(arr, n);

    int pos, val;

    printf("\nEnter position to insert (0 to %d): ", n);
    scanf("%d", &pos);

    printf("Enter value to insert: ");
    scanf("%d", &val);

    if (pos >= 0 && pos <= n && n < capacity) {

        for (int i = n; i > pos; i--) {
            arr[i] = arr[i - 1];
        }

        arr[pos] = val;
        n++;

        printf("Array after Insertion:\n");
        displayArray(arr, n);

    } else {
        printf("Invalid Position!\n");
    }

    int delPos;

    printf("\nEnter position to delete (0 to %d): ", n - 1);
    scanf("%d", &delPos);

    if (delPos >= 0 && delPos < n) {

        for (int i = delPos; i < n - 1; i++) {
            arr[i] = arr[i + 1];
        }

        n--;

        printf("Array after Deletion:\n");
        displayArray(arr, n);

    } else {
        printf("Invalid Position!\n");
    }

    return 0;
}

#include <iostream>
using namespace std;

void displayArray(int arr[], int size) {
    cout << "[ ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << "]" << endl;
}

int main() {

    int capacity = 100;
    int arr[100];
    int n = 0;

    cout << "How many elements initially? ";
    cin >> n;

    cout << "Enter " << n << " elements:" << endl;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }

    cout << "Initial Array:" << endl;
    displayArray(arr, n);

    int pos, val;

    cout << "\nEnter position to insert (0 to " << n << "): ";
    cin >> pos;

    cout << "Enter value to insert: ";
    cin >> val;

    if (pos >= 0 && pos <= n && n < capacity) {

        for (int i = n; i > pos; i--) {
            arr[i] = arr[i - 1];
        }

        arr[pos] = val;
        n++;

        cout << "Array after Insertion:" << endl;
        displayArray(arr, n);

    } else {
        cout << "Invalid Position!" << endl;
    }

    int delPos;

    cout << "\nEnter position to delete (0 to " << (n - 1) << "): ";
    cin >> delPos;

    if (delPos >= 0 && delPos < n) {

        for (int i = delPos; i < n - 1; i++) {
            arr[i] = arr[i + 1];
        }

        n--;

        cout << "Array after Deletion:" << endl;
        displayArray(arr, n);

    } else {
        cout << "Invalid Position!" << endl;
    }

    return 0;
}

Limitations of Arrays

While arrays are fast, they have drawbacks:

  • Fixed Size: You must know the size beforehand. If you need more space, you have to create a new, larger array and copy everything over.
  • Insertion/Deletion Overhead: Removing an element from the beginning requires shifting all other elements, which is slow ($O(n)$).

Solution: For dynamic sizing, Java developers often use ArrayList, which handles resizing automatically.