Advertisement
Md_Sakib_Hossain

Insertion On Indexed Array

Mar 9th, 2021
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2. public class InsertIA{
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         int N=5;
  6.         int J=N; //step 1
  7.         int[] LA = new int[20];
  8.         int positionOfElement = 2;
  9.         int ITEM = 99;
  10.         for (int i=0;i<J;i++) {      //scan part not most important
  11.             LA[i]=scan.nextInt();
  12.         }
  13.         for (int i=0;i<J;i++) {
  14.             System.out.print(LA[i]+" ");
  15.         }
  16.  
  17.         while(J>(positionOfElement - 1)){ //step 2
  18.             LA[J]=LA[J-1]; //step 3 this statement is important
  19.             J--;//step 4
  20.         }
  21.         LA[positionOfElement-1] = ITEM; //step 5
  22.         System.out.print("\n");
  23.  
  24.         N=N+1; //step 6
  25.  
  26.         for (int i=0;i<N;i++) {
  27.             System.out.print(LA[i]+" "); //just print it
  28.         }
  29.     }
  30. }
  31.  
  32. /// main code is///
  33.  
  34. /*
  35.  
  36. while(J>(positionOfElement - 1)){ //step 2
  37.             LA[J]=LA[J-1]; //step 3 this statement is important
  38.             J--;//step 4
  39.         }
  40.         LA[positionOfElement-1] = ITEM; //step 5
  41.  
  42. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement