1. 使用`Arrays.toString()`方法:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(arr));
}
}
2. 使用for循环遍历数组:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
3. 使用增强for循环遍历数组:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
4. 使用`Arrays.deepToString()`方法输出多维数组:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(Arrays.deepToString(arr));
}
}
5. 使用Java 8的Stream API输出数组:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
Arrays.stream(arr).forEach(System.out::println);
}
}
6. 使用`String.join()`方法输出数组元素,以指定的分隔符连接:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
System.out.println(String.join(", ", arr));
}
}