
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Move All Zeros to the End of Array in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to move all the zeros to the end of array i.e. if an array contains n number of zeros in it then all the n number of zeros will be pushed back in an array.
Let's explore the article to see how it can be done by using Java programming language.
To Show you Some Instances
Instance-1
Suppose the original array is {128, 0, 99, 67, 50, 0, 29, 7, 0}
After moving all the zeros to the end of array the result will be:
Elements of array after moving all the zeros to the end of array:
128 99 67 50 29 7 0 0 0
Instance-2
Suppose the original array is {23, 9, 6, 4, 0, 0, 21, 7, 0, 6, 0, 9}
After moving all the zeros to the end of array the result will be:
Elements of array after moving all the zeros to the end of array:
23 9 6 4 21 7 6 9 0 0 0 0
Instance-3
Suppose the original array is {3, 9, 5, 1, 0, 0, 11, 6, 0, 9}
After moving all the zeros to the end of array the result will be:
Elements of array after moving all the zeros to the end of array:
3 9 5 1 11 6 9 0 0 0
Algorithm
Algorithm-1 (By Using Sorting)
Step 1 ? Declare and initialize an integer array.
Step 2 ? Implement the logic for multiple approaches.
Step 3 ? Sort the array in descending order and hence zeros will get at the last position.
Step 4 ? Print the elements of the array.
Algorithm-2 (By Manual Iteration and Replacement)
Step 1 ? Declare and initialize an integer array.
Step 2 ? Implement the logic for multiple approaches.
Step 3 ? Push all non-zero elements in the left side of an array with the zero element remaining in the end.
Step 4 ? Print the elements of the array.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it ?
array.length
where, ?array' refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Sorting
By Manual Iteration and Replacement
Let's see the program along with its output one by one.
Approach-1: By Using Sorting
In this approach, array elements will be initialized in the program. Then as per the algorithm sort the array in descending order and hence zeros will get at the last position.
Example
import java.util.*; public class Main { public static void main(String[] args){ //Declare and initialize the array elements int array[] = {128, 0, 99, 67, 50, 0, 29, 7, 0}; //getting length of an array int n = array.length; //calling user defined function func(array, n); } //user defined method public static void func(int array[], int n) { //sorting the array elements Arrays.sort(array); System.out.println("Elements of array after moving all the zeros to the end of array: "); //prints array using the for loop for (int i = n-1; i >= 0; i--) { System.out.print(array[i] + " "); } } }
Output
Elements of array after moving all the zeros to the end of array: 128 99 67 50 29 7 0 0 0
Approach-2: By Manual Iteration and Replacement
In this approach, array elements will be initialized in the program. Then as per algorithm push all non-zero elements in the left side of an array with the zero-element remaining in the end.
Example
import java.io.*; public class Main { public static void main (String[] args){ //Declare and initialize the array elements int arr[] = {3, 9, 5, 1, 0, 0, 11, 6, 0, 9}; //getting length of an array int n = arr.length; //calling user defined method func(arr, n); } //user defined method static void func(int arr[], int n) { // Count of non-zero elements int count = 0; //shifting non zero element to the left of the loop for (int i = 0; i < n; i++) if (arr[i] != 0) arr[count++] = arr[i]; //adding the zeros to the end while (count < n) arr[count++] = 0; //printing the result System.out.println("Elements of array after moving all the zeros to the end of array: "); for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } }
Output
Elements of array after moving all the zeros to the end of array: 3 9 5 1 11 6 9 0 0 0
In this article, we explored how to move all the zeros to the end of array by using Java programming language.