溫馨提示×

java怎么打印輸出二維數組

小億
111
2024-09-13 13:50:29
欄目: 編程語言

在Java中,你可以使用嵌套的for循環來遍歷并打印二維數組的每個元素。以下是一個示例:

public class Main {
    public static void main(String[] args) {
        // 創建一個 3x4 的二維數組
        int[][] arr = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };

        // 獲取數組的行數和列數
        int rows = arr.length;
        int cols = arr[0].length;

        // 使用嵌套的for循環遍歷并打印二維數組的每個元素
        for (int i = 0; i< rows; i++) {
            for (int j = 0; j< cols; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println(); // 換行
        }
    }
}

運行上述代碼,將輸出以下結果:

1 2 3 4
5 6 7 8
9 10 11 12

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女