In the previous article, we have seen Java Program to Count Positive Negative and Zero from Array
In this article we are going to see how we can separate negative, positive and zero elements in separate arrays by using Java programming language.
Java Program to Separate Positive Negative and Zero elements from Array and Store in Separate arrays
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to separate negative, positive and zero elements in separate arrays.
Method-1: Java Program to Separate Negative, Positive and Zero Elements in Separate Arrays By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Iterate the array.
- If array element is greater than zero then it is positive number so store in the positive array.
- If array element is smaller than zero then it is negative number so store in the negative array.
- Else element is equal to zero then it is zero so store in the zero array.
- Then print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Array with elements int arr[] = {10,-15,1,-30,50,7,1,0,0}; System.out.println("The array elements are : "); //For Loop to print the elements for(int iter=0;iter<arr.length;iter++) { System.out.print(arr[iter]+" "); } //variable to store positive,negative and zero elements in separate arrays //declaring the arrays with size of actual array int positive[] = new int[arr.length]; int negative[] = new int[arr.length]; int zero[] = new int[arr.length]; int i=0; int j=0; int k=0; //Loop to count positive,negative and zero elements for(int row=0;row<arr.length;row++) { //if array element is greater than zero it is positive if(arr[row]>0) { //storing the positive value in positive array positive[i]=arr[row]; i++; } //if array element is smaller than zero it is negative if(arr[row]<0) { //storing the negative value in negative array negative[j]=arr[row]; j++; } //if array element is not greater or smaller than zero then it is equal to zero if(arr[row]==0) { //storing the zero value in zero array zero[k]=arr[row]; k++; } } System.out.println("\nPositive array : "); printArray(positive,i); System.out.println("\nNegative array : "); printArray(negative,j); System.out.println("\nZero Array : "); printArray(zero,k); } //printArray() method to print array public static void printArray(int[] arr, int counter) { for(int a=0;a<counter;a++) { System.out.print(arr[a]+" "); } } }
Output: The array elements are : 10 -15 1 -30 50 7 1 0 0 Positive array : 10 1 50 7 1 Negative array : -15 -30 Zero Array : 0 0
Method-2: Java Program to Separate Negative, Positive and Zero Elements in Separate Arrays By Dynamic Initialization of Array Elements
Approach:
- Take the array size input from user.
- Take the input of array elements.
- Print the array.
- Then Iterate the array.
- If array element is greater than zero then it is positive number so store in the positive array.
- If array element is smaller than zero then it is negative number so store in the negative array.
- Else element is equal to zero then it is zero so store in the zero array.
- Then print the result.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Array with elements int arr[] = null; System.out.print("Enter the length of the array : "); int length = scan.nextInt(); arr = new int[length]; int iter; // Entering the array elements System.out.println("Enter the array elements : "); for(iter=0;iter<arr.length;iter++) arr[iter]=scan.nextInt(); System.out.println("The array elements are : "); //For Loop to print the elements for(iter=0;iter<arr.length;iter++) { System.out.print(arr[iter]+" "); } //variable to store positive,negative and zero elements in separate arrays //declaring the arrays with size of actual array int positive[] = new int[arr.length]; int negative[] = new int[arr.length]; int zero[] = new int[arr.length]; int i=0; int j=0; int k=0; //Loop to count positive,negative and zero elements for(int row=0;row<arr.length;row++) { //if array element is greater than zero it is positive if(arr[row]>0) { //storing the positive value in positive array positive[i]=arr[row]; i++; } //if array element is smaller than zero it is negative if(arr[row]<0) { //storing the negative value in negative array negative[j]=arr[row]; j++; } //if array element is not greater or smaller than zero then it is equal to zero if(arr[row]==0) { //storing the zero value in zero array zero[k]=arr[row]; k++; } } System.out.println("\nPositive array : "); printArray(positive,i); System.out.println("\nNegative array : "); printArray(negative,j); System.out.println("\nZero Array : "); printArray(zero,k); } //printArray() method to print array public static void printArray(int[] arr, int counter) { for(int a=0;a<counter;a++) { System.out.print(arr[a]+" "); } } }
Output: Enter the length of the array : 6 Enter the array elements : 1 2 -3 -4 0 0 The array elements are : 1 2 -3 -4 0 0 Positive array : 1 2 Negative array : -3 -4 Zero Array : 0 0
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs:
- Java Program to Find the Elements from an Array which are Greater than a Given Number
- Java Program to Find the Elements from an Array which are Smaller than a Given Number
- Java Program to Find the Elements from the Array which are Greater than the Average of Array
- Java Program to Find the Elements from the Array which are Smaller than the Average of Array