Accessing elements of an array in Java SE 22 is done in the same way as in earlier versions of Java. You use the index notation to access individual elements of the array. Here’s a simple example:
public class Main { public static void main(String[] args) { // Declaring and initializing an array int[] numbers = {1, 2, 3, 4, 5}; // Accessing elements of the array System.out.println("First element: " + numbers[0]); System.out.println("Second element: " + numbers[1]); System.out.println("Third element: " + numbers[2]); System.out.println("Fourth element: " + numbers[3]); System.out.println("Fifth element: " + numbers[4]); } }
Output:
First element: 1 Second element: 2 Third element: 3 Fourth element: 4 Fifth element: 5
In this example, `numbers[0]` accesses the first element of the array `numbers`, `numbers[1]` accesses the second element, and so on. Remember that array indices in Java are zero-based, so the first element is at index 0, the second at index 1, and so forth.