Best 2 ways to find the nth element in Array Java

In this post, you can learn how to find the nth element in array Java. We have given our best 2 ways to find the elements.

Simple Logic:

We can ask the user which position of element he/she wants to see. Once the user will input the position of the element then by using that element with the help of a simple if condition and length variable we can show that particular element to the user.

How to find the nth element in array Java?

We can find the nth element in array Java by using the below two approaches.

  1. Find nth element in Array by using if condition.
  2. Find nth element in Array by using for loop.

Also Read: How to convert List to Byte Array in Java? – Easy way

Approach-1: Find the nth element in Array Java by using if condition

We can find any element of any Array by using a simple if condition and predefined length variable.

First, we need to accept the nth position of Array that we need to find and then using that position we can find nth element of Array.

Let’s see the program now.

/**
 * How to find the nth element in an array in Java?
 */

package cf.java.queshub;

import java.util.Scanner;

public class FindElementInArray {

	public static void main(String[] args) {
		// Declare any Array 
		int[] ar = {15,17,8,25,9,30};                    
		Scanner sc = new Scanner(System.in);
		// Enter position of element
		System.out.print("Enter nth position of element: ");
		int pos = sc.nextInt();
		// Check user given position is valid or not
		if(pos <= ar.length){
			// Get particular element
			int element = ar[pos-1];
			System.out.println(pos + "th element of given Array is "+ element);
		}                     
		else {
			System.out.println("Invalid Position! Please enter position upto " + ar.length);
	   }

	}

}
Code language: PHP (php)

Output:

Enter nth position of element: 5
5th element of given Array is 9Code language: JavaScript (javascript)

Explanation of above program:

In the above program, we have just used a simple if condition to check whether the user given position is a valid position or not by using if(pos <= ar.length).

Finally, we can get the particular element from Array (int element = ar[pos-1];) and display that element by using that particular user given position. Otherwise, we can display to the user that user given position number is invalid.

Also Read: How to convert Array to XML in Java – 2 easy way

Approach-2: Find nth element in Array by using for loop

We can find any element of any Array by using a simple for loop and if condition variable.

First, we need to accept the nth position of Array that we need to find and then using that position we can find nth element of Array.

Let’s see the program now.

/**
 * How to find the nth element in Array Java by using for loop?
 */
package cf.java.array;

import java.util.Scanner;

public class FindElementInJavaUsingForLoop {

	public static void main(String[] args) {
		// Declare any Array
		int[] ar = { 15, 17, 8, 25, 9, 30 };
		Scanner sc = new Scanner(System.in);
		// Enter position of element
		System.out.print("Enter nth position of element: ");
		int pos = sc.nextInt();
		// Check user given position is valid or not
		boolean check = false;
		for (int i = 0; i < ar.length; i++) {
			if (pos == i + 1) {
				System.out.println("Position " + pos + " element of given Array is " + ar[i]);
				check = true;
			}
		}
		if (!check)
			System.out.println("Please enter a valid position between 1 to " + ar.length);
	}

}
Code language: PHP (php)

Output:

Enter nth position of element: 5
Position 5 element of given Array is 9
Code language: JavaScript (javascript)

Explanation of above program:

In the above program, we have declared an array int[] ar = { 15, 17, 8, 25, 9, 30 }; and then we have accepted which position of element user wants to get.

We have declared a check variable to check whether the user given position is a valid position or not. (boolean check = false;)

We have used a for loop to iterate the given Array and inside for loop, we will check whether the given position is equal to element position on each iteration(if (pos == i + 1). When we find the particular position equal to the element position through iteration, we will display the element to the user and change the value of the check to true.

If we don’t found the given position in the Array or given position is invalid, then we will display user that given position is invalid and please input a valid position to get particular element.

Also Read: How to convert Array to XML in Java – 2 Palindrome In Java(String & Number) – Check 2 Easy Methods

FAQs

How to print the position of an element of an array in Java?

You can print the position of an element in Array by using a simple for loop in Java. You can ask the user to give the element of which he/she wants the position and then by using that element we can show the position like the below example.

for (int i = 0; i < ar.length; i++) {
if (ar[i]== userGivenElement) {
System.out.println(“Position of the given “+userGivenElement+” is “+i);
}
}

How to find nth element in Array?

We can find the nth element in array Java by using the below two approaches.
1. Find nth element in Array by using if condition.
2. Find nth element in Array by using for loop.

How do I find the second last number in an array?

We can find the second last number in an array by using the simple length variable.

int[] ar = { 15, 17, 8, 25, 9, 30 };
int secondLastNumber = ar[ar.length-2]; System.out.println("Second Last Number is "+secondLastNumber);

How do I print the second element of an array?

You can find second element of an array by using the index 1.

int[] ar = { 15, 17, 8, 25, 9, 30 };
int secondElement = ar[1]; System.out.println("Second element is "+secondElement);

3 thoughts on “Best 2 ways to find the nth element in Array Java”

Leave a Comment