How to Print Java Array Elements
In Java, loops can be used to iterate through all elements in an array and print them out. Here is a simple example:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
// 使用for循环输出数组中的所有元素
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
In this example, we defined an array arr containing 5 integers, then used a for loop to iterate through all elements in the array and used the System.out.println() method to print them to the console. Running the above code will output:
1
2
3
4
5