English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How do you separate zero from non-zero integers in an integer array using Java?

To separate zeros from non-zero elements in an integer array and push them to the end, you need to reorder its array by assigning all non-zero elements to their positions in order starting from zero. Then, fill zeros from the last position of the array to the end.

Example

The following Java program pushes all zeros in the array to the end.

import java.util.Arrays;
import java.util.Scanner;
public class ZerosFromNonZeros {
   public static void main(String args[]){
      //Read array from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the size of the array that is to be created: ");
      int size = sc.nextInt();
      int[] myArray = new int[size];
      System.out.println("Enter the elements of the array: ");
      for(int i=0; i<size; i++{
         myArray[i] = sc.nextInt();
      }
      System.out.println("The array created is: ");+Arrays.toString(myArray));
      System.out.println("Resultant array: ");
      int pos = 0;
      for(int i = 0; i < myArray.length;++{
         if(myArray[i]!=0){
            myArray[pos]=myArray[i];
            pos++;
         }
      }
      while(pos < myArray.length) {
         myArray[pos] = 0;
         pos++;
      }
      System.out.println("The array created is: ");+Arrays.toString(myArray));
   }
}

Output result

Enter the size of the array that is to be created:
8
Enter the elements of the array:
14
0
56
0
12
47
0
0
The array created is: [14, 0, 56, 0, 12, 47, 0, 0]
Resultant array:
The array created is: [14, 56, 12, 47, 0, 0, 0, 0

Place zero in the same way at the beginning of the array, iterate over the elements of the array backward, and arrange each non-zero element in the array in turn from the last position. Finally, fill the remaining positions with zeros.

Reorder its array by assigning all non-zero elements to their positions in order starting from zero. Then, fill zeros from the last position of the array to the end.

Example

The following Java program pushes all zeros in the array to the beginning.

import java.util.Arrays;
import java.util.Scanner;
public class ZerosFromNonZeros {
   public static void main(String args[]){
      //Read array from user
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the size of the array that is to be created: ");
      int size = sc.nextInt();
      int[] myArray = new int[size];
      System.out.println("Enter the elements of the array: ");
      for(int i=0; i<size; i++{
         myArray[i] = sc.nextInt();
      }
      System.out.println("The array created is: ");+Arrays.toString(myArray));
      System.out.println("Resultant array: ");
      int pos = myArray.length-1;
      for(int i = myArray.length-1; i>=0; i--{
         if(myArray[i]!=0){
            myArray[pos]=myArray[i];
            pos--;
         }
      }
      while(pos>=0) {
         myArray[pos] = 0;
         pos--;
      }
      System.out.println("The array created is: ");+Arrays.toString(myArray));
   }
}

Output result

Enter the size of the array that is to be created:
8
Enter the elements of the array:
14
0
56
0
12
47
0
0
The array created is: [14, 0, 56, 0, 12, 47, 0, 0]
Resultant array:
The array created is: [0, 0, 0, 0, 14, 56, 12, 47]