Java Program to Print nth Element of the Array

In the previous article, we have seen Java Program to Increment Each Element of Array by a Specified Number

In this article we are going to see how to print nth element of an array using Java programming language.

Java Program to Print nth Element of the 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 print nth element of the array.

Method-1: Java Program to Print nth Element of the Array By Static Initialization of Array Elements

Approach:

  • Declare and Initialize the array.
  • Print the nth element of the array using array indexing.

Program:

public class Main 
{
    public static void main(String[] args)
    {
        // initializing array
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        // initializing nth element
        int n = 3;
        // calling printNthElement() method
        printNthElement(arr, n);
    }

    //printNthElement() method to find the nth element 
    //by using the array index
    static void printNthElement(int[] arr, int n) {
        // printing the method
        System.out.println("Nth element of the array = " + arr[n - 1]);
    }
}
Output:

Nth element of the array = 3

Method-2: Java Program to Print nth Element of the Array By Dynamic Initialization of Array Elements

Approach:

  1. Create scanner class object.
  2. Ask use length of the array.
  3. Initialize the array with given size.
  4. Ask the user for array elements.
  5. Initialize the array.
  6. Print the nth element of the array using array indexing.

Program:

import java.util.Scanner;

public class Main
{

    public static void main(String[] args) 
    {
        // initializing array
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.print("Enter position of the element to be printed : ");
        int num  = sc.nextInt();
        // calling method
        printNthElement(arr, num);
    }

    static void printNthElement(int[] arr, int n) 
    {
        // printing the method
        System.out.println("Element at position " + n + " = " + arr[n - 1]);
    }
}
Output:

Enter the size of array: 7
Enter array elements: 10 20 30 40 50 60 70
Enter position of the element to be printed : 4
Element at position 4 = 40

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Read Also:

  1. How to print one element of an array in java?
  2. Get nth element of list java?
  3. Print array elements in java using scanner?
  4. Java program to print the elements of an array in reverse order?
  5. How to print elements of array in java?
  6. Find index of element in array java?
  7. How to print array elements in java using for loop?
  8. How to get nth element of an array in javascript?
  9. How to print elements of array in java?
  10. Find index of element in array java?
  11. How to get nth element of an array in javascript?
  12. How to print one element of an array in java?
  13. Print array elements in java using scanner?
  14. Java program to print nth prime number?
  15. Java program to print the elements of an array?
  16. Java program to print the number of elements present in an array?
  17. How to print every nth element from an array in java?
  18. Java program to print the elements of an array present on even position?

Related Java Programs: