Java Matrix Print printMatrix(int[][] M)

Here you can find the source of printMatrix(int[][] M)

Description

print Matrix

License

Open Source License

Declaration

public static void printMatrix(int[][] M) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static void printMatrix(int[][] M) {
        if (M.length == 0 || M[0].length == 0)
            return;
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                System.out.printf("%5d", M[i][j]);
            }//from  w w  w . j ava  2 s  . co m
            System.out.println();
        }
    }

    public static void printMatrix(boolean[][] M) {
        if (M.length == 0 || M[0].length == 0)
            return;
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                System.out.printf("%5d", M[i][j] ? 1 : 0);
            }
            System.out.println();
        }
    }

    public static void printMatrix(char[][] M) {
        if (M.length == 0 || M[0].length == 0)
            return;
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                System.out.printf("%5c", M[i][j]);
            }
            System.out.println();
        }
    }

    public static void printMatrix(Object[][] M, int padding) {
        if (M.length == 0 || M[0].length == 0)
            return;
        for (int i = 0; i < M.length; i++) {
            for (int j = 0; j < M[0].length; j++) {
                System.out.printf("%" + padding + "s", M[i][j].toString());
            }
            System.out.println();
        }
    }
}

Related

  1. printMatrix(float[] array, int rows, int columns)
  2. printMatrix(int M, int N, double mat[][])
  3. printMatrix(int m, int n, double[][] a)
  4. printMatrix(int matrix[][])
  5. printMatrix(int[] array, String format, int width)
  6. printMatrix(int[][] matrix)
  7. printMatrix(int[][] s)
  8. printMatrix(int[][] target)
  9. printMatrix(Object[][] matrix)