📜  在Java中打印二维数组或矩阵

📅  最后修改于: 2022-05-13 01:55:37.143000             🧑  作者: Mango

在Java中打印二维数组或矩阵

先决条件: Java中的数组, Java中的数组声明(单维和多维)

方法一(简单遍历)

我们可以使用 mat.length 找到矩阵 mat[][] 中的行数。要查找第 i 行中的列数,我们使用 mat[i].length。

Java
// Java program to print the elements of
// a 2 D array or matrix
import java.io.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int i = 0; i < mat.length; i++)
 
            // Loop through all elements of current row
            for (int j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}


Java
// Java program to print the elements of
// a 2 D array or matrix using for-each
import java.io.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)
 
            // Loop through all columns of current row
            for (int x : row)
                System.out.print(x + " ");
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}


Java
// Java program to print the elements of
// a 2 D array or matrix using toString()
import java.io.*;
import java.util.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)
 
            // converting each row as string
            // and then printing in a separate line
            System.out.println(Arrays.toString(row));
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}


Java
// Java program to print the elements of
// a 2 D array or matrix using deepToString()
import java.io.*;
import java.util.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        System.out.println(Arrays.deepToString(mat));
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}


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

方法 2(使用for-each 循环

这与上述类似。我们在这里使用 for 每个循环,而不是简单的 for 循环。

Java

// Java program to print the elements of
// a 2 D array or matrix using for-each
import java.io.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)
 
            // Loop through all columns of current row
            for (int x : row)
                System.out.print(x + " ");
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}
输出
1 2 3 4 5 6 7 8 9 10 11 12 

方法 3(使用Arrays.toString()以矩阵样式打印

Arrays.toString(row) 将完整的行转换为字符串,然后将每一行打印在单独的行中。

Java

// Java program to print the elements of
// a 2 D array or matrix using toString()
import java.io.*;
import java.util.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)
 
            // converting each row as string
            // and then printing in a separate line
            System.out.println(Arrays.toString(row));
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}
输出
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

方法 4(使用 Arrays.deepToString() 以矩阵样式打印)

Arrays.deepToString(int[][]) 一步将二维数组转换为字符串。

Java

// Java program to print the elements of
// a 2 D array or matrix using deepToString()
import java.io.*;
import java.util.*;
class GFG {
 
    public static void print2D(int mat[][])
    {
        System.out.println(Arrays.deepToString(mat));
    }
 
    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}
输出
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]