Skip to content

Commit ead6ae8

Browse files
committed
feat: Implement element insertion at a specific index in an array with manual shifting [array-insertion-basic]
📌 Core Concept: Demonstrates how to insert a new element into an existing array by shifting elements manually to make space at a given index. 🧮 Program Overview: - Initializes an integer array `A` with a fixed capacity. - Populates the first 6 elements manually. - Prints the original array state. - Inserts element `20` at index `2` by: - Shifting elements one position to the right starting from the end. - Placing the new value at the specified index. - Prints the updated array. 🧪 Behavior: - Original: `3, 9, 7, 8, 12, 6` - After insertion at index 2: `3, 9, 20, 7, 8, 12` 📌 Key Features: - Demonstrates low-level control over array manipulation. - Highlights index-based shifting to manage fixed-size arrays. ✅ Useful for: - Understanding static array limitations in Java. - Teaching manual memory shifting before using higher-level structures (like `ArrayList`). Signed-off-by: Somesh diwan <[email protected]>
1 parent 26710e3 commit ead6ae8

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

Section9ArrayAB/Array IMP/InsertElementInArray.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class InsertElementInArray {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
int A[] = new int[10];
54

65
A[0]=3; A[1]=9; A[2]=7; A[3]=8; A[4]=12; A[5]=6;
@@ -24,4 +23,4 @@ public static void main(String[] args)
2423
System.out.println("");
2524

2625
}
27-
}
26+
}

0 commit comments

Comments
 (0)