Java Program to Find the Middle Element of an Array

In the previous article, we have seen Java Program to Divide an Arrays in Two Arrays

In this article we are going to see how we can find the middle element of an array in Positive Mid Element Code In Java

Java Program to Find the Middle Element of 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.

Recommended Reading On: Java Program to Copy an Array in Reverse

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 the middle element of the array. Here are some ways how to find middle element of array in java and how to print middle element of array in java. There are two different ways. They are:

Method-1: Java Program to Find the Middle Element of an Array By Static Initialization of Array Elements

Approach

  • Create and initialize an array.
  • Display the array to the user.
  • Find out the difference between the last and first index of the array.
  • Divide it by 2 then add it to the firstIndex. Store the resultant.
  • Print the resultant and the element at that index.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30};
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(arr));

        int startIndex = 0, lastIndex = arr.length - 1;
        // Setting the mid index
        int midIndex = startIndex + (lastIndex-startIndex)/2;
        System.out.println("The mid index is "+midIndex+" and the element at mid is "+arr[midIndex]);
    }
}

Output:

The array elements are : [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30]
The mid index is 7 and the element at mid is 8

Method-2: Java Program to Find the Middle Element of an Array By Dynamic Initialization of Array Elements

Approach

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Display the array to the user.
  • Find out the difference between the last and first index of the array.
  • Divide it by 2 then add it to the firstIndex. Store the resultant.
  • Print the resultant and the element at that index.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        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: "); 
        for (int i = 0; i < num; i++) 
        { 
            arr[i] = sc.nextInt(); 
        }
        
        // Printing the array
        System.out.println("The array elements are : "+Arrays.toString(arr));

        int startIndex = 0, lastIndex = arr.length - 1;
        // Setting the mid index
        int midIndex = startIndex + (lastIndex-startIndex)/2;
        System.out.println("The mid index is "+midIndex+" and the element at mid is "+arr[midIndex]);
    }
}

Output:

Enter the number of elements in the array: 6
Enter the elements: 1 2 3 4 5 6
The array elements are : [1, 2, 3, 4, 5, 6]
The mid index is 2 and the element at mid is 3

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills. This is how to get middle element of array.

Related Java Programs: