
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Move All the 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.
- Related Articles
- How to move all the zeros to the end of the array from the given array of integer numbers using C#?
- Move all zeros to start and ones to end in an Array of random integers in C++
- Move all zeroes to end of array in C++
- Move all zeroes to end of the array using List Comprehension in Python
- In-place Move Zeros to End of List in Python
- Move all zeros to the front of the linked list in C++
- In-place Algorithm to Move Zeros to End of List in JavaScript
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Move the first row to the end of the JTable in Java Swing
- Minimum move to end operations to make all strings equal in C++
- Moving all zeroes present in the array to the end in JavaScript
- Python Program to move numbers to the end of the string
- JavaScript program for Minimum move to end operations to make all strings equal
- Moving all Upper case letters to the end of the String using Java RegEx
- How to move specific item in array list to the first item in Java?
