Home  >  Article  >  Java  >  How to loop through all elements using Java array

How to loop through all elements using Java array

PHPz
PHPzforward
2023-04-22 10:58:17891browse

Explanation

1. Get each element from the array separately, that is, traverse. In array operations, traversal is also the cornerstone.

2. Each element in the array can be widely used, but if there are many array elements, this writing method will definitely not work, so it needs to be changed to a loop writing method.

Example

The array index is 0-lenght-1, which can appear as a loop condition.

public class ArrayTest01 {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 };
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
}
}

The above is the detailed content of How to loop through all elements using Java array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete