Delete an element from an array

import java.util.Scanner;
 class Insert_Array 
{
    public static void main(String[] args) 
    {
        int n, pos, x;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n+1];
        System.out.println("Enter all the elements:");
        for(int i = 0; i < n; i++)
        {
            a[i] = s.nextInt();
        }
        System.out.print("Enter the position where you want to insert element:");
        pos = s.nextInt();
        System.out.print("Enter the element you want to insert:");
        x = s.nextInt();
        for(int i = pos; i < n; i++)
        {
            a[i] = a[i+1];
        }
        //a[n-1] = x;
        System.out.print("After inserting:");
        for(int i = 0; i < n-1; i++)
        {
            System.out.print(a[i]+",");
        }
        //System.out.print(a[n]);
    }
}

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...