Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could write a convenience method:</p> <pre><code>public PrintStream print(String format, Object... arguments) { return System.out.format(format, arguments); } </code></pre> <p>But as you can see, you've simply just renamed <code>format</code> (or <code>printf</code>).</p> <p>Here's how you could use it:</p> <pre><code>private void printScores(Player... players) { for (int i = 0; i &lt; players.length; ++i) { Player player = players[i]; String name = player.getName(); int score = player.getScore(); // Print name and score followed by a newline System.out.format("%s: %d%n", name, score); } } // Print a single player, 3 players, and all players printScores(player1); System.out.println(); printScores(player2, player3, player4); System.out.println(); printScores(playersArray); // Output Abe: 11 Bob: 22 Cal: 33 Dan: 44 Abe: 11 Bob: 22 Cal: 33 Dan: 44 </code></pre> <p>Note there's also the similar <code>System.out.printf</code> method that behaves the same way, but if you peek at the implementation, <code>printf</code> just calls <code>format</code>, so you might as well use <code>format</code> directly.</p> <ul> <li><a href="http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html" rel="noreferrer">Varargs</a></li> <li><a href="http://download.oracle.com/javase/7/docs/api/java/io/PrintStream.html#format%28java.lang.String,%20java.lang.Object...%29" rel="noreferrer"><code>PrintStream#format(String format, Object... args)</code></a></li> <li><a href="http://download.oracle.com/javase/7/docs/api/java/io/PrintStream.html#printf%28java.lang.String,%20java.lang.Object...%29" rel="noreferrer"><code>PrintStream#printf(String format, Object... args)</code></a></li> </ul>
 

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