Monday, August 12, 2019

BlueJ Program on Deletion of an Element From Array





Another program on BlueJ Array To delete an element of array (location to be taken from the user) 

import java.io.*;
class Arr
{
 int arr[]=new int[50];
int i,n,loc;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws IOException
{
System.out.println ("How many elements:");
 n=Integer.parseInt(br.readLine());



 for(i=0;i< n;i++)
 {
System.out.println ("Value:");
  arr[i]=Integer.parseInt(br.readLine());
  }
System.out.println ("The values are:");
for(i=0;i< n;i++)
{
System.out.print (" " + arr[i]);
}
System.out.println ("Enter the location which value is to be deleted:");
loc=Integer.parseInt(br.readLine());
for(i=loc-1;i< n;i++)
{
 arr[i]=arr[i+1];
}
System.out.println ("Now the array is(after deletion).\n");
for(i=0;i< n-1;i++)
System.out.println (" " + arr[i]);
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner