在Java中,有多種方式可以遍歷數(shù)組,包括:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.println(num);
}
int[] arr = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(arr));
int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr).forEach(num -> System.out.println(num));
int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr).map(num -> num * 2).forEach(num -> System.out.println(num));