Here you can find the source of printMatrix(int[] array, String format, int width)
public static void printMatrix(int[] array, String format, int width)
//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; } }