在Java中,有多种方式可以遍历数组,包括:
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));