Java Program to Insert an Element in a Specified Position in a Given Array. An array is a collection of similar data elements stored at contiguous memory locations. Enter size of array and then enter all the elements of that array. Now enter the element you want to insert and position where you want to insert that element. We shift all the elements in the array from that location by one and hence insert the element easily.
import java.util.Arrays; public class Insert_element_array { public static void main(String args[]) { //Program to insert a element in specific index of an array int[] a = {10,20,30,40,50,60,70,80,90,100}; int index = 2; int value = 55; System.out.println("Before Insert "+Arrays.toString(a) ); for(int i=a.length-1;i>index;i--) { a[i]=a[i-1]; } a[index]=value; System.out.println("After Insert "+Arrays.toString(a) ); } }
Before Insert [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] After Insert [10, 20, 55, 30, 40, 50, 60, 70, 80, 90]To download raw file Click Here
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions