In array insertion, we can insert new element into first, end position or even in middle position of an array.
Insert an element at given user’s position of an array.
importjava.util.*; import java.util.Scanner; import java.io.*; class main1{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); intpos,x,cnt=0,j=0; int array[] = new int[10]; // array length = 10 array[0]=21; array[1]=11; array[2]=-67; array[3]=-90; array[4]=4; System.out.println(“\n Enter position of element for insert : “); pos = sc.nextInt(); int g; for (x=0;x<10 ;x++ ) { g = array[x]; if (g>0 || g<0) { cnt++; } } intloopStart = pos; intloopEnd=cnt; intp,q; p=loopEnd; q=loopEnd-1; for (j=loopStart;j<loopEnd ;j++ ) { array[p] = array[q]; p–; q–; } System.out.println(“\n Enter value for this position : “); array[pos] = sc.nextInt(); int z; for (z=0;z<loopEnd+1;z++ ) { System.out.println(“\t “+ array[z]); } } } |

Insert an element in particular position of an array, which already sorted in ascending order.
importjava.util.*; import java.util.Scanner; import java.io.*; class main2{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); intpos=0,x,cnt=0,j=0; int array[] = new int[10]; // array length = 10 array[0]=11; array[1]=21; array[2]=31; array[3]=40; array[4]=60; int g; for (x=0;x<10 ;x++ ) { g = array[x]; if (g>0 || g<0) { cnt++; } } System.out.println(“\n Enter value : “); intval = sc.nextInt(); int loop1 = cnt-1; for(x=0;x<loop1;x++){ if(array[x]<val&&val<array[x+1]){ pos=x+1; } } System.out.println(“\n index of inserted value will “+pos); intloopStart = pos; intloopEnd=cnt; intp,q; p=loopEnd; q=loopEnd-1; for (j=loopStart;j<loopEnd ;j++ ) { array[p] = array[q]; p–; q–; } array[pos]=val; int z; for (z=0;z<loopEnd+1;z++ ) { System.out.println(“\t “+ array[z]); } } } |

Insert an element at the end of an array(1D array)
import java.util.Scanner; class main3{ public static void main(String[] args){ int array[] = new int[10]; array[0]=11; array[1]=21; array[2]=90; array[3]=-9; array[4]=3; intcnt=0; for (int x=0;x<10 ; x++) { if (array[x]<0 || array[x]>0) { cnt=cnt+1; } } System.out.println(“\n enter element to add the end : “); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); array[cnt]=n; for (int y=0;y<cnt+1 ;y++ ) { System.out.println(“\t”+array[y]); } } } |

Insert element at first position of an array.
import java.util.Scanner; class main4{ public static void main(String[] args){ int array[] = new int[10]; int x; array[0]=11; array[1]=21; array[2]=90; array[3]=-9; array[4]=3; intcnt=0; for (x=0;x<10 ; x++) { if (array[x]<0 || array[x]>0) { cnt=cnt+1; } } System.out.println(“\n before insertion array was \n “); for (x=0;x<cnt ;x++ ) { System.out.println(“\t”+array[x]); } System.out.println(“\n enter element to add first position : “); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); intlastIndex=cnt; int loop=cnt+1,p=cnt-1,q=cnt,i; System.out.println(“\n after insertion of element at first position, array becomes “); for (i=0;i<loop-1;i++ ) { array[q]=array[p]; p–; q–; } array[0]=n; for (int y=0;y<cnt+1 ;y++ ) { System.out.println(“\t”+array[y]); } } } |
