Arrays are one of the most commonly used data structures in programming. While storing data in an array is simple, inserting a new element into an array requires a little more work.
In this blog, we’ll understand:
- β What is Array Insertion?
- β How insertion works internally
- β Java program for array insertion
- β Step-by-step explanation
- β What happens when the array is full?
- β Time Complexity of Array Insertion
Let’s begin!
π What is Array Insertion?
Array insertion is the process of adding a new element into an array at a specific position.
Suppose we have the following array:
Index : 0 1 2 3
Array : 10 20 30 40
Now we want to insert 25 at index 2.
The final array should become:
Index : 0 1 2 3 4
Array : 10 20 25 30 40
Notice something important.
The elements 30 and 40 have moved one position to the right to make space for 25.
This movement is called shifting.
π€ Why Do We Need to Shift Elements?
Arrays store their elements in contiguous (continuous) memory locations.
There cannot be an empty gap between two elements.
Suppose we directly place 25 at index 2.
10 20 25 40
Where did 30 go?
It gets overwritten!
Therefore, before inserting a new element, we first move all the elements after the insertion position one step to the right.
π Visual Representation
Before Insertion
+----+----+----+----+----+
|10 |20 |30 |40 | |
+----+----+----+----+----+
Insert 25 at index 2.
Step 1: Shift Elements
40 β Move Right
30 β Move Right
Now the array becomes
+----+----+----+----+----+
|10 |20 | |30 |40 |
+----+----+----+----+----+
Step 2: Insert New Element
+----+----+----+----+----+
|10 |20 |25 |30 |40 |
+----+----+----+----+----+
Insertion is complete!
π» Java Program for Array Insertion
public class ArrayInsertion {
public static void main(String[] args) {
int arr[] = new int[6];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
int size = 4;
int position = 2;
int value = 25;
// Shift elements one position to the right
for (int i = size; i > position; i--) {
arr[i] = arr[i - 1];
}
// Insert new element
arr[position] = value;
size++;
// Display array
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
}
}
Output
10 20 25 30 40
π Step-by-Step Execution
Initial Array
10 20 30 40 _
Current Size = 4
First Iteration
Move 40 to the next position.
10 20 30 40 40
Second Iteration
Move 30 to the next position.
10 20 30 30 40
Insert New Value
Place 25 at index 2.
10 20 25 30 40
Insertion completed successfully.
β οΈ What Happens If the Array Is Full?
This is one of the most important concepts in arrays.
Suppose we have an array of size 5.
Capacity = 5
Index
0 1 2 3 4
Array
10 20 30 40 50
The array is completely full.
Now we want to insert 60.
Can we do it?
No!
There is no empty space available.
Unlike many other data structures, arrays have a fixed size.
Once an array is created, its size cannot be increased.
For example,
int arr[] = new int[5];
This array will always have exactly 5 positions.
You cannot make it hold a sixth element.
π Solution 1: Create a Bigger Array
If the array is full, we can create a new array with a larger size.
int[] oldArray = {10, 20, 30, 40, 50};
int[] newArray = new int[10];
Now copy all elements.
for(int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
Insert the new element.
newArray[5] = 60;
Now the new array becomes
10 20 30 40 50 60
The old array remains unchanged.
π» Complete Java Program
public class BiggerArrayExample {
public static void main(String[] args) {
int[] oldArray = {10, 20, 30, 40, 50};
int[] newArray = new int[10];
for(int i = 0; i < oldArray.length; i++) {
newArray[i] = oldArray[i];
}
newArray[5] = 60;
for(int i = 0; i < 6; i++) {
System.out.print(newArray[i] + " ");
}
}
}
Output
10 20 30 40 50 60
π Solution 2: Use an ArrayList
Java provides a dynamic data structure called ArrayList.
Unlike normal arrays, an ArrayList automatically grows when it becomes full.
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
System.out.println(list);
}
}
Output
[10, 20, 30, 40, 50, 60]
Internally, an ArrayList creates a larger array, copies the existing elements, inserts the new element, and then continues working seamlessly.
β±οΈ Time Complexity of Array Insertion
The time required for insertion depends on where the element is inserted.
| Insertion Position | Time Complexity |
|---|---|
| At the beginning | O(n) |
| In the middle | O(n) |
| At the end (space available) | O(1) |
Why O(n)?
In the worst case, every element has to be shifted one position to the right before the new element can be inserted.
Therefore, insertion takes linear time.
π Key Takeaways
- Arrays have a fixed size.
- Insertion means adding a new element at a specific position.
- Before insertion, elements must be shifted to the right to create space.
- If the array is full, insertion is not possible unless a larger array is created.
- Java’s
ArrayListsolves this problem by automatically resizing itself whenever more space is required. - The worst-case time complexity of array insertion is O(n) because shifting elements takes linear time.
π― Final Thoughts
Understanding array insertion is essential before learning advanced data structures such as linked lists, stacks, queues, and dynamic arrays. The key idea is that arrays occupy continuous memory, which makes accessing elements very fast but inserting new elements comparatively expensive due to the need for shifting.
Once you’re comfortable with insertion, you’ll find it much easier to understand other array operations like deletion, searching, and sorting.