A complete program: Delete an element from an array
For String programs clickhttps://wordpress-343193-1101484.cloudwaysapps.com/string-programs-part-5
Write a program to delete an element from an array. Ask the user to enter and the position(index) from where you want to delete an element.
For String programs click
String Programs part 5
import java.io.*;
class delete
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the length of the array");
int n=Integer.parseInt(br.readLine());
int Arr[]=new int[n];
System.out.println("Enter the values");
for(int i=0;i<n;i++)
Arr[i]=Integer.parseInt(br.readLine());
System.out.println("Enter the index of the value to be deleted");
int index=Integer.parseInt(br.readLine());
for(int i=index+1;i<n;i++)
{
Arr[i-1]=Arr[i];
}
n--;
System.out.println("THE ARRAY AFTER DELETION");
for(int i=0;i<n;i++)
{
System.out.print(Arr[i]+"\t");
}
}
}Output:
Enter the length of the array
4
Enter the values
21
33
78
44
Enter the index of the value to be deleted
2
THE ARRAY AFTER DELETION
21
33
44
For other java videos
https://www.youtube.com/user/trushnatej
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.