Java Program for Array Insertion

Here is a simple java program for Array insertion.

import java.io.*;
class array_insertion
{
public static void main(String args[]) throws IOException
{
int r,pos,num,i;
int a[ ]=new int [20];
int b[ ]=new int [20];
DataInputStream z=new DataInputStream(System.in);
System.out.println(“Enter the Range”);
r=Integer.parseInt(z.readLine());
System.out.println(“Enter the array”);
for(i=0;ia[i]=Integer.parseInt(z.readLine());
System.out.println(“Enter the position”);
pos=Integer.parseInt(z.readLine());
System.out.println(“Enter the number”);
num=Integer.parseInt(z.readLine());
System.out.println(“The array after insertion”);
for(i=r-1;i>=pos-1;i–)
{
a[i+1]=a[i];
}
a[pos-1]=num;
for(i=0;i<=r;i++)
System.out.println(a[i]);
}
}

I am not sure about the program. Got this from one of my friends.

Add Comment