Java Matrix Print printMatrix(int[] array, String format, int width)

Here you can find the source of printMatrix(int[] array, String format, int width)

Description

print Matrix

License

BSD License

Declaration

public static void printMatrix(int[] array, String format, int width) 

Method Source Code

//package com.java2s;
/**// w ww. j  av a2  s . c o m
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * @author Jay Codec
 * 
 */

public class Main {
    public static void printMatrix(int[] array, String format, int width) {
        String[] strings = new String[array.length];
        int maxLen = 0;
        for (int i = 0; i < array.length; i++) {
            strings[i] = String.format(format, array[i]);
            maxLen = Math.max(maxLen, strings[i].length());
        }
        for (int ind = 0; ind < strings.length;) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < width && ind < strings.length; i++, ind++) {
                for (int j = 0; j < maxLen - strings[ind].length() + 1; j++)
                    builder.append(' ');
                builder.append(strings[ind]);
            }
            System.out.println(builder);
        }
    }

    public static int max(int[] array) {
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }

        return max;
    }
}

Related

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