Java Program to insert element to an array at specified position
Program
import java.util.Arrays;
import java.util.Scanner;
public class InsertElementsToArrayAtPosition {
public static void main(String[] args) {
int newElement;
int i, j;
int position;
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
int inputArray[] = new int[size + 1];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
System.out.print("Enter the element which you want to insert: ");
newElement = reader.nextInt();
System.out.print("Enter the position where you want to insert: ");
position = reader.nextInt();
System.out.print("Printing array elements after inserting : ");
for (j = inputArray.length - 1; j > position; j--) {
inputArray[j] = inputArray[j - 1];
}
inputArray[position] = newElement;
System.out.println(Arrays.toString(inputArray));
}
}
This Java program demonstrates how to insert an element into an array at a specified position.
-
Input Array Size and Elements:
- The user is prompted to input the size of the array (
size
). - An integer array
inputArray[]
is initialized with a size ofsize + 1
to allow space for the new element. - The user enters the elements of the array, which are stored in the first
size
positions ofinputArray
.
- The user is prompted to input the size of the array (
-
Input New Element and Position:
- The user is prompted to input the new element to be inserted (
newElement
). - The user specifies the position (
position
) where the element should be inserted. This position is 0-based.
- The user is prompted to input the new element to be inserted (
-
Shift Elements:
- To make space for the new element, a
for
loop iterates backward from the last position (inputArray.length - 1
) to the specified position. - Each element is shifted one position to the right.
- To make space for the new element, a
-
Insert Element:
- The new element is assigned to the specified position in the array.
-
Display Array:
- The updated array, including the newly inserted element, is printed using
Arrays.toString()
.
- The updated array, including the newly inserted element, is printed using
Key Points:
- Position Handling:
- The position must be valid (between
0
andsize
). Additional validation can be added for error handling.
- The position must be valid (between
- Shifting Elements:
- The shifting operation ensures that no data is overwritten while making space for the new element.
- Static Array:
- The array size is fixed after creation, and this program works for fixed-sized arrays without using dynamic data structures like
ArrayList
.
- The array size is fixed after creation, and this program works for fixed-sized arrays without using dynamic data structures like
Output
javac InsertElementsToArrayAtPosition.java
java InsertElementsToArrayAtPosition
Enter the required size of the array:
3
Enter the elements of the array:
11
22
44
Enter the element which you want to insert: 33
Enter the position where you want to insert: 2
Printing array elements after inserting : [11, 22, 33, 44]