Thursday, May 25, 2017

Program in java to delete an element in an unsorted array

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package datastrucarraya;
import java.util.Scanner;
/**
 *
 * @author arpis
 */
public class deleteinsorted {
     public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
        System.out.println(" enter the number of elements in your array");
        int i =sc.nextInt();
        int ar[] = new int[i];
        // taking the input of the arrays
        System.out.println("enter the elements of the arrays");
        for(int j=0;j<(i);j++){
            ar[j]=sc.nextInt();
        }
          int l= i-1;
         System.out.println("enter the number to be deleted");
         int delnum=sc.nextInt();
         for(int j=0;j<(i);j++){
            if(ar[j]==delnum){
              ar[j]=(ar[j]-ar[j]);
             
                if(j==l){
                    System.out.println("new array with the deleted element is");
                    for(int ii=0;ii<(l);ii++){
                           System.out.println(ar[ii]);
        }
                }
                else{
                    for(int k=ar[j];k<l;k++){
                        ar[k]=ar[k+1];
                       
                    }
                    System.out.println("new array with the deleted element is");
                      for(int iii=0;iii<(l);iii++){
                           System.out.println(ar[iii]);
                }
               
            }
        }
        
}}
}

No comments:

Post a Comment