How to Access Java Array Elements

To retrieve values from a Java array, you can use array indexes, which represent the position of elements in the array, to access specific values. For example, if you have an array named arr, to access the first element, you can use arr[0]. Here is a simple example:

int[] arr = {1, 2, 3, 4, 5};

// 访问第一个元素
int firstElement = arr[0];
System.out.println("第一个元素是:" + firstElement);

// 访问第三个元素
int thirdElement = arr[2];
System.out.println("第三个元素是:" + thirdElement);

Please note that the index of an array starts at 0. Therefore, the index of the first element is 0, the second element is 1, and so on.

bannerAds