In the previous article, we have seen Java Program to Delete All the Odd Elements from the Array of Integers
In this article we will see how to delete all the element ‘0’ from an integer array.
Java Program to Delete Element ‘0’ if it is Present in an Integer Array
Prerequisite:
See below articles to know more about Array, array declaration, array instantiation and array initialization.
- How to Declare an Array in Java?
- How to Instantiate an Array in Java?
- How to Initialize an Array in Java?
Let’s see different ways how to delete all negative elements from an array.
Method-1: Java Program to Delete Element ‘0’ if it is Present in an Integer Array Without Using an Extra Array
Approach:
- Create scanner class object.
- Ask user for input of array length.
- Initialize the array with given size.
- Ask the user for array elements.
- Display the original array
arr[]
. - Now call
deleteZero()
user defined method. - Inside this method declare a variable
'p'=0
- Then iterate the array(by for loop) and check if any positive or negative element is found then store that in
arr[p++]
. Means we are not storing elements having value as 0. - After the completion of the iteration(for loop) we will see
arr[]
now contains all positive or negative numbers except 0 where'p'
represents the new length of array. - Now print the new array of length
'p'
.
Method:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } // Displaying the array System.out.print("Original array : "); //printing the array for(int i = 0; i < arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); //calling the deleteNegative() method //to check negative elements in array and delete that int k=deleteZero(arr); System.out.print("After deleting elements '0' from array : "); //printing the array for(int i = 0; i < k; i++) System.out.print(arr[i]+" "); System.out.println(); } //deleteZere() method public static int deleteZero(int[] arr) { int p = 0; //iterating the array for(int i = 0; i <arr.length; i++) { //if element is positive or negative //then only we are storing that in arr[p], in same input array //which means we did not store elements having value as 0 //so at last in the array all positive and negative numbers will be available and 0's will be deleted if(arr[i] > 0 || arr[i] < 0) { arr[p++] = arr[i]; } } // input array holding the output data // 'p' is the final length of new array return p; } }
Output: Enter the number of elements in the array: 6 Enter the elements: 1 0 2 3 0 4 Original array : 1 0 2 3 0 4 After deleting elements '0' from array : 1 2 3 4
Method-2: Java Program to Delete Element ‘0’ if it is Present in an Integer Array By Using an Extra Array
Approach:
- Approach is completely same as like above logic the only difference is that here we are storing the result in an extra array rather than the same input array.
Method:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); int arr[] = new int[num]; System.out.print("Enter the elements: "); for (int i = 0; i < num; i++) { arr[i] = sc.nextInt(); } // Displaying the array System.out.print("Original array : "); //printing the array for(int i = 0; i < arr.length; i++) System.out.print(arr[i]+" "); System.out.println(); //calling the deleteZero() method //to check elements 0 in array and delete that deleteZero(arr); } //deleteZero() method public static void deleteZero(int[] arr) { int[] outputArray = new int[arr.length]; int p = 0; //iterating the array for(int i = 0; i < arr.length; i++) { //if element is positive or negative //then only we are storing that in outputArray[p] //which means we did not store elements having value as 0 //so at last in the array all positive and negative numbers will be available and 0's will be deleted if(arr[i] > 0) { outputArray[p++] = arr[i]; } } System.out.print("After deleting elements 0 from array : "); //printing the array for(int i = 0; i < p; i++) System.out.print(outputArray[i]+" "); System.out.println(); } }
Output: Enter the number of elements in the array: 8 Enter the elements: 1 2 0 0 5 6 0 8 Original array : 1 2 0 0 5 6 0 8 After deleting elements 0 from array : 1 2 5 6 8
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Articles:
- Java Program to Find all Elements in Array which have at-least Two Greater Elements
- Java Program to Find all Elements in Array which have at-least Two Smaller Elements
- Java Program for Mean and Median of an Unsorted Array
- Java Program to Find All the Array Elements Which are Greater than a Specified Number