Java Academy · Lesson

Insert, Delete by Index & Shift

Insert into a new array, delete by index with left shift, and practice safe bounds checks.

Lesson 3 of 38 steps

Insert, Delete by Index & Shift is a free Java Academy lesson on CoddyKit. This is lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, and your progress syncs across the web and the CoddyKit app. The Java Academy course includes 3 lessons in total.

Insert Overview

Insert into a plain array usually creates a new array with one extra slot. Copy elements before the position, place the new value, then copy the rest shifted by one.

Insert New Array

Insert at index i into a new array:

public class Main {
  // Method to insert a value at a given index
  static int[] insertAt(int[] a, int i, int val) {
    // Create a new array, one element larger
    int[] b = new int[a.length + 1];

    // Copy elements before the insertion index
    for (int p = 0; p < i; p = p + 1) {
      b[p] = a[p];
    }

    // Insert the new value at position i
    b[i] = val;

    // Copy the rest of the elements after position i
    for (int p = i; p < a.length; p = p + 1) {
      b[p + 1] = a[p];
    }

    return b;
  }

  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12};

    // Insert 99 at index 2 (between 8 and 9)
    int[] b = insertAt(a, 2, 99);

    // Print new array
    System.out.print("Array after insertion: ");
    for (int x : b) {
      System.out.print(x + " ");
    }
  }
}

Delete & Shift

Delete by index by shifting left and clearing the last cell:

public class Main {
  // Method to delete an element at index k
  static void deleteAt(int[] a, int k) {
    // Shift elements to the left, starting from index k
    for (int i = k; i < a.length - 1; i = i + 1) {
      a[i] = a[i + 1];
    }

    // Clear the last element (since it's now duplicated)
    a[a.length - 1] = 0; 
  }

  public static void main(String[] args) {
    // Example array
    int[] a = {5, 8, 9, 12, 15};

    // Delete element at index 2 (value = 9)
    deleteAt(a, 2);

    // Print updated array
    System.out.print("Array after deletion: ");
    for (int x : a) {
      System.out.print(x + " ");
    }
  }
}

Bounds Check

Bounds check helps avoid runtime errors. Valid index for n elements is 0..n-1. For insert, valid i is 0..n.

public class Main {
  // Insert a value at a given index
  static int[] insertAt(int[] a, int i, int val) {
    // Validate index (0 ≤ i ≤ a.length is allowed for insert)
    if (i < 0 || i > a.length) {
      throw new IllegalArgumentException("bad index");
    }

    int[] b = new int[a.length + 1];
    for (int p = 0; p < i; p++) b[p] = a[p];
    b[i] = val;
    for (int p = i; p < a.length; p++) b[p + 1] = a[p];
    return b;
  }

  // Delete a value at a given index
  static void deleteAt(int[] a, int k) {
    // Validate index (0 ≤ k < a.length is required for delete)
    if (k < 0 || k >= a.length) {
      throw new IllegalArgumentException("bad index");
    }

    for (int i = k; i < a.length - 1; i++) {
      a[i] = a[i + 1];
    }
    a[a.length - 1] = 0; // clear the last slot
  }

  public static void main(String[] args) {
    int[] a = {5, 8, 9, 12};

    // Test insert
    int[] b = insertAt(a, 2, 99);
    System.out.print("After insert: ");
    for (int x : b) System.out.print(x + " ");

    System.out.println();

    // Test delete
    deleteAt(b, 3);
    System.out.print("After delete: ");
    for (int x : b) System.out.print(x + " ");
  }
}

Delete New Array

Create a smaller copy that skips index k:

public class Main {
  // Method to create a new array without the element at index k
  static int[] without(int[] a, int k) {
    // Validate index
    if (k < 0 || k >= a.length) {
      throw new IllegalArgumentException("bad index");
    }

    // New array has one fewer element
    int[] b = new int[a.length - 1];

    // Copy elements before k
    for (int p = 0; p < k; p = p + 1) {
      b[p] = a[p];
    }

    // Copy elements after k (shift left by one)
    for (int p = k + 1; p < a.length; p = p + 1) {
      b[p - 1] = a[p];
    }

    return b;
  }

  public static void main(String[] args) {
    int[] a = {5, 8, 9, 12, 15};

    // Remove element at index 2 (value = 9)
    int[] b = without(a, 2);

    // Print original
    System.out.print("Original array: ");
    for (int x : a) System.out.print(x + " ");
    System.out.println();

    // Print new array
    System.out.print("Array without index 2: ");
    for (int x : b) System.out.print(x + " ");
  }
}

Insert/Delete Demo

Run it: See new-array insert, in-place delete with left shift, and delete by copy.

public class Main {
  static int[] insertAt(int[] a, int i, int val) {
    if (i < 0 || i > a.length) throw new IllegalArgumentException("bad index");
    int[] b = new int[a.length + 1];
    for (int p = 0; p < i; p = p + 1) b[p] = a[p];
    b[i] = val;
    for (int p = i; p < a.length; p = p + 1) b[p + 1] = a[p];
    return b;
  }

  static void deleteAtShift(int[] a, int k) {
    if (k < 0 || k >= a.length) throw new IllegalArgumentException("bad index");
    for (int i = k; i < a.length - 1; i = i + 1) {
      a[i] = a[i + 1];
    }
    a[a.length - 1] = 0;
  }

  static int[] without(int[] a, int k) {
    if (k < 0 || k >= a.length) throw new IllegalArgumentException("bad index");
    int[] b = new int[a.length - 1];
    for (int p = 0; p < k; p = p + 1) b[p] = a[p];
    for (int p = k + 1; p < a.length; p = p + 1) b[p - 1] = a[p];
    return b;
  }

  static String join(int[] a) {
    String s = "";
    for (int i = 0; i < a.length; i = i + 1) {
      s = s + a[i] + (i + 1 < a.length ? " " : "");
    }
    return s;
  }

  public static void main(String[] args) {
    int[] a = {10, 20, 30, 40};
    System.out.println("a           = " + join(a));

    int[] b = insertAt(a, 2, 99);
    System.out.println("insertAt(2) = " + join(b));

    deleteAtShift(a, 1);
    System.out.println("deleteAt(1) = " + join(a));

    int[] c = without(b, 3);
    System.out.println("without(3)  = " + join(c));
  }
}

Delete & Shift Check

Quick check: Which snippet deletes at index k by shifting left and clears the last slot?

Recap & Next

Recap: You inserted with a new array, deleted by left shift, and created smaller copies. Always check bounds.

Next: Move on to arrays of objects and simple algorithms like counting frequencies.

Free to start

Learn Java with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
104
Lessons
374

Frequently Asked Questions

Is the “Insert, Delete by Index & Shift” lesson free?

Yes — the full text of “Insert, Delete by Index & Shift” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO. The Java Academy course includes 3 lessons in total.

What will I learn in “Insert, Delete by Index & Shift”?

Insert into a new array, delete by index with left shift, and practice safe bounds checks. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 3.

How long does the “Insert, Delete by Index & Shift” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Array Basics & Iteration
  2. Searching, Min/Max & Reverse
  3. Insert, Delete by Index & Shift
← Back to Java Academy