Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want that specific control over formatting, you have to do it to each item individually. There is <code>Arrays.toString()</code>, but it provides no control over individual elements.</p> <p>If you are printing multiple arrays and you want their output to "line up", you will need to choose output formats so that happens, e.g.:</p> <pre><code> float data1[] = new float[] { 1, 2.5f, 30, 4.56f, 5 }; int data2[] = new int[] { 1, 22, 33, -4, 5 }; for (float f:data1) System.out.printf("%6.2f ", f); System.out.println(); for (int n:data2) System.out.printf("%3d ", n); System.out.println(); </code></pre> <p>Which outputs:</p> <pre><code> 1.00 2.50 30.00 4.56 5.00 1 22 33 -4 5 </code></pre> <p>That example might not be the precise format you are going for, but the point is, you have control this way. If you want to do left-aligned numbers, wider fields, if your types are Strings or floats or doubles or ints, etc.</p> <p>Sometimes it helps to work out field widths and padding spaces in e.g. a text editor or on paper first. E.g. with the above, where we try to line up decimal points and digits between floats and integers, I like to first work out the format in a fixed-width editor (like in a comment or in notepad), e.g.:</p> <pre><code>FFF.FF FFF.FF FFF.FF FFF.FF III III III III </code></pre> <p>Then you can more easily see how that translates to format specifiers:</p> <ul> <li><code>FFF.FF&nbsp;</code> is <code>"%6.2f&nbsp;"</code>.</li> <li><code>III&nbsp;&nbsp;&nbsp;&nbsp;</code> is <code>"%3d&nbsp;&nbsp;&nbsp;&nbsp;"</code>.</li> </ul> <p>See the documentation for <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html" rel="nofollow"><code>Formatter</code></a> for more information about the format codes you can pass to <code>System.out.printf()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload