OneCompiler

[Java] How to get 3rd item from the last in an array?

I want to read 3rd element from the last of an array into a variable, how can we do that in Java?

1 Answer

4 years ago by

We can use aray[aray.length - 3] to find the 3rd item from the last. Following is the complete program

public class JavaArrays {
	public static void main(String[] args) {
		String[] aray = { "blue", "red", "green", "yellow" };
		System.out.println(aray[aray.length - 3]);
	}
}

Output:

red

Try this program online here: https://onecompiler.com/java/3xmt9wq7s

4 years ago by Karthik Divi