Java Program to Find the Indexes of Element ‘0’ Present in Array

In the previous article, we have seen Java Program to Delete All 0 Element Values from an Array of Integers

In this article we are going to find index of an array element using Java programming language.

Java Program to Find the Indexes of Element ‘0’ Present in 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 find to find index of element 0 present in array.

Method-1: Java Program to Find the Indexes of Element ‘0’ Present in Array By Static Initialization of Array Elements

Approach:

  • Create an array with elements which is the original array i.e arr[].
  • Create another array of size as like original array to store the indices where 0 is present.
  • Take a for loop iterate the original array.
  • Check if anywhere element 0 is found then store that index in the index[] array.
  • Print the index[] array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        //Array declared with array elements
       int arr[] ={1,2,3,0,4,5,0,6};
        
        System.out.print("Original Array: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
        //declaring int varibale item and assigning value 0
        int item = 0;
        
        //Another array declared to store the indices where 0 is present
        int index[]= new int[arr.length];
        int j=0;
        // Traversinng the array looking for the element
        for(int i = 0; i<arr.length; i++)
        {
            if(arr[i]==item)
            {
                index[j] = i;
                j++;
            }

        }
        // Printing the final result
        if(j == 0)
        {
            System.out.println("Element 0 is not present in array");
        }
        else
        {
            System.out.println("Element "+item+" is present at index: ");
            //printing array containg indices of 0 in the original array
            for(int i = 0; i < j; i++)
                System.out.print(index[i]+" ");
            System.out.println();
        }
    }
}
Output:

Original Array: 1 2 3 0 4 5 0 6 
Element 0 is present at index: 
3 6

Method-2: Java Program to Find the Indexes of Element ‘0’ Present in Array By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the original array i.e arr[].
  • Initialize the array with given size.
  • Ask the user for input of  array elements to the original array.
  • Create another array of size as like original array to store the indices where 0 is present.
  • Take a for loop iterate the original array.
  • Check if anywhere element 0 is found then store that index in the index[] array.
  • Print the index[] array.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: ");
        //taking input of array elements
        for (int i = 0; i < num; i++) 
        { 
        arr[i] = sc.nextInt(); 
        }
        
        System.out.print("Original Array: ");
        //printing the original array
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
        
        //declaring int varibale item and assigning value 0
        int item = 0;
        
        //Another array declared to store the indices where 0 is present
        int index[]= new int[arr.length];
        int j=0;
        // Traversinng the array looking for the element
        for(int i = 0; i<arr.length; i++)
        {
            if(arr[i]==item)
            {
                index[j] = i;
                j++;
            }

        }
        // Printing the final result
        if(j == 0)
        {
            System.out.println("Element 0 is not present in array");
        }
        else
        {
            System.out.println("Element "+item+" is present at index: ");
            //printing array containg indices of 0 in the original array
            for(int i = 0; i < j; i++)
                System.out.print(index[i]+" ");
            System.out.println();
        }
    }
}
Output:

Enter the number of elements in the array: 9
Enter the elements: 2 0 7 0 3 4 0 9 6 
Original Array: 2 0 7 0 3 4 0 9 6 
Element 0 is present at index: 
1 3 6

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: