温馨提示×

java怎么打印数组中所有的元素

小亿
86
2024-02-04 16:02:37
栏目: 编程语言

要打印数组中所有的元素,可以使用for循环遍历数组,并使用System.out.println()方法打印每个元素。

以下是一个示例代码:

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]);
        }
    }
}

输出结果:

1
2
3
4
5

0