In the previous article, we have seen Java Program to Print All the Negative Elements in an Array
In this article we are going to see how to print all the positive elements in an array using Java language.
Java Program to Print All the Positive Elements in an Array
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 and print all the positive elements in an array
Method-1: Java Program to Print All the Positive Elements in an Array By Static Initialization of Array Elements
Approach:
- Create an array with elements which is the original array and one blank arrays of same size which is the result array.
- Display the array elements to the user.
- Pass both the arrays into an user function
negative()
that separates the elements by traversing through the array and storing positive elements in the result array. - Print the result array.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating the original array int arr[] = {1, -2, 3, -4, 5}; //result array int res[] = new int[arr.length]; // Prints the array elements System.out.println("The array elements are "+ Arrays.toString(arr)); //calling positive() method positive(arr,res); } // positive() method which separates positive elements of the array // and stores it in another array static void positive(int arr[], int res[]) { int count=0; // Separating the array of positive elements for(int i=0;i<arr.length;i++) { if(arr[i]>0) { res[count]=arr[i]; count++; } } System.out.print("The positive array elements are : "); // Prints negative element array for(int i=0;i<count;i++) System.out.print(res[i]+" "); } }
Output: The array elements are [1, -2, 3, -4, 5] The positive array elements are : 1 3 5
Method-2: Java Program to Print All the Positive Elements in an Array By Dynamic Initialization of Array Elements
Approach:
- Create an array by taking array elements as user input which is the original array and one blank arrays of same size which is the result array.
- Display the array elements to the user.
- Pass both the arrays into an user function
negative()
that separates the elements by traversing through the array and storing positive elements in the result array. - Print the result array.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Taking size as input from the user System.out.print("Enter the array size :"); int size = scan.nextInt(); // Creating the original array int arr[] = new int[size]; // Entering the array elements System.out.print("Enter array elements : "); for(int i=0;i<size;i++) { arr[i] = scan.nextInt(); } //result array int res[] = new int[arr.length]; // Prints the array elements System.out.println("The array elements are "+ Arrays.toString(arr)); //calling positive() method positive(arr,res); } // positive() method which separates positive elements of the array // and stores it in another array static void positive(int arr[], int res[]) { int count=0; // Separating the array of positive elements for(int i=0;i<arr.length;i++) { if(arr[i]>0) { res[count]=arr[i]; count++; } } System.out.print("The positive array elements are : "); // Prints negative element array for(int i=0;i<count;i++) System.out.print(res[i]+" "); } }
Output: Enter the array size :5 Enter array elements : 1 2 -3 -4 5 The array elements are [1, 2, -3, -4, 5] The positive array elements are : 1 2 5
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: