Print all elements in array java – Java Program to Print All the Negative Elements in an Array

Print all elements in array java: In the previous article, we have seen Java Program to Check if the Given Arrays are Disjoint

In this article we are going to see how to print all the negative elements in an array using Java language.

Java Program to Print All the Negative Elements in an Array

How to print all elements of an array in java: 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 negative elements in an array

Method-1: Java Program to Print All the Negative 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 negative 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[] = {12, 22, -34, 22, 54, -6, 52, 8, -9, 34, 54, -68, -10, 20, -30};
        //result array
        int res[] = new int[arr.length];
        // Prints the array elements
        System.out.println("The array elements are "+ Arrays.toString(arr));
        
        //calling negative() method
        negative(arr,res);

    }
    
    // negative() method which separates negative elements of the array
    // and stores it in another array
    static void negative(int arr[], int res[])
    {
        int count=0;
        // Separating the array of negative elements
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]<=0)
            {
                res[count]=arr[i];
                count++;
            }
               
        }

        System.out.print("\nThe negative array elements are : ");
        // Prints negative element array
        for(int i=0;i<count;i++)
            System.out.print(res[i]+" ");
        
    }
}
Output:

The array elements are [12, 22, -34, 22, 54, -6, 52, 8, -9, 34, 54, -68, -10, 20, -30]
The negative array elements are : -34 -6 -9 -68 -10 -30

Method-2: Java Program to Print All the Negative 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 negative 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 negative() method
        negative(arr,res);

    }
    
    // negative() method which separates negative elements of the array
    // and stores it in another array
    static void negative(int arr[], int res[])
    {
        int count=0;
        // Separating the array of negative elements
        for(int i=0;i<arr.length;i++)
        {
            if(arr[i]<=0)
            {
                res[count]=arr[i];
                count++;
            }
               
        }

        System.out.print("The negative 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 negative array elements are : -1 -2 -3

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: