What is the best way to print the cells of a String[][] array as a right-justified table? For example, the input
{ { 'x', 'xxx' }, { 'yyy', 'y' }, { 'zz', 'zz' } }
should yield the output
x xxx yyy y zz zz
This seems like something that one should be able to accomplish using java.util.Formatter, but it doesn’t seem to allow non-constant field widths. The best answer will use some standard method for padding the table cells, not the manual insertion of space characters.
Indeed, if you specify a width for the fields, it should be right-justified.
If you need to have a dynamic padding, minimal for the longest string, you have to walk the array, getting the maximal width, generate the format string with the width computed from this maxima, and use it for format the output.