Print Array in Java: Step-by-Step Guide
To print all elements in an array, you can use a for loop to iterate through the array and use the System.out.println() method to print each element.
Here is a sample code:
public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
// 使用for循环遍历数组
for (int i = 0; i < array.length; i++) {
// 打印数组中的元素
System.out.println(array[i]);
}
}
}
The output results:
1
2
3
4
5