In this tutorial you will learn about the Java Program to Remove Element From Array and its application with practical example.
Java Program to Remove the elements of an Array
In this tutorial, we will learn to create a Java program that will remove the elements of an Array using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Arithmetic operations in Java Programming.
Program description to find Remove the Elements of an Array.
In this program, we will first take the input array size and the elements from the user. Then we will take a position to remove an element of the array using a for-loop. At last, we will print the array to the user using the print function.
With the help of this program, we can Remove the Elements of an Array.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.*;//importing the package for the program. class Arry { public static void main(String args[]) { Scanner sc = new Scanner(System.in); //Declaring the required variable for the program. int i,n,ps ; //Taking the number of element for the array System.out.println("Enter the number of elements:") ; n = sc.nextInt(); int[] a = new int[n]; //Taking the elements of the array System.out.println("Enter the elements") ; for(i=0;i<n;i++) { a[i] = sc.nextInt(); } //Taking the position to delete the element System.out.println("Enter the position of the number which is to be deleted"); ps = sc.nextInt(); for(i=ps;i<n-1;i++) { a[i]=a[i+1]; } n=n-1; //Printing the array after the deletion. System.out.println("\nOn deleting new array we get is\n"); for(i=0;i<n;i++) { System.out.println("a["+i+"] = "+a[i]); } } } |
Output:-
In the above program, we have first initialized the required variable.
- ps = it will hold the integer value of the input elements.
- i = it will hold the integer value.
- n = it will hold the integer value of the input.
Input size and the elements of the array from the user.
Program Logic Code for the Remove of the elements of the array.
Printing the array after deletion of the element.