Java Matrix Print printMatrix(Object[][] matrix)

Here you can find the source of printMatrix(Object[][] matrix)

Description

Prints a matrix to console

License

Apache License

Parameter

Parameter Description
matrix Matrix to be printed

Return

Printed string

Declaration

public static String printMatrix(Object[][] matrix) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from   w  ww  .  j  av a  2s  .  co  m
     * Prints a matrix to console
     * @param matrix Matrix to be printed
     * @return Printed string
     */
    public static String printMatrix(Object[][] matrix) {
        StringBuilder builder = new StringBuilder("");
        int rowCount = matrix.length;

        if (rowCount == 0) {
            System.out.println("Matrix is empty");
            return "Matrix is empty";
        }
        int columnCount = matrix[0].length;

        for (int i = 0; i < rowCount; i++) {
            builder.append("[");
            for (int j = 0; j < columnCount; j++) {
                builder.append(matrix[i][j]).append(j == columnCount - 1 ? "" : ",");
            }
            builder.append("]\n");
        }
        System.out.println(builder.toString());
        return builder.toString();
    }
}

Related

  1. printMatrix(int[] array, String format, int width)
  2. printMatrix(int[][] M)
  3. printMatrix(int[][] matrix)
  4. printMatrix(int[][] s)
  5. printMatrix(int[][] target)
  6. printMatrix(Object[][] matrix)
  7. printMatrix(Object[][] matrix)
  8. printMatrix(PrintStream out, double[][] matrix)
  9. printMatrixGeneric(boolean[][] matrix)