Note that there are some explanatory texts on larger screens.

plurals
  1. POClearest way to comma-delimit a list?
    text
    copied!<p>What is the clearest way to comma-delimit a list in Java?</p> <p>I know several ways of doing it, but I'm wondering what the best way is (where "best" means clearest and/or shortest, not the most efficient.</p> <p>I have a list and I want to loop over it, printing each value. I want to print a comma between each item, but not after the last one (nor before the first one).</p> <pre><code>List --&gt; Item ( , Item ) * List --&gt; ( Item , ) * Item </code></pre> <p>Sample solution 1:</p> <pre><code>boolean isFirst = true; for (Item i : list) { if (isFirst) { System.out.print(i); // no comma isFirst = false; } else { System.out.print(", "+i); // comma } } </code></pre> <p>Sample solution 2 - create a sublist:</p> <pre><code>if (list.size()&gt;0) { System.out.print(list.get(0)); // no comma List theRest = list.subList(1, list.size()); for (Item i : theRest) { System.out.print(", "+i); // comma } } </code></pre> <p>Sample solution 3:</p> <pre><code> Iterator&lt;Item&gt; i = list.iterator(); if (i.hasNext()) { System.out.print(i.next()); while (i.hasNext()) System.out.print(", "+i.next()); } </code></pre> <p>These treat the first item specially; one could instead treat the last one specially.</p> <p>Incidentally, here is how <code>List</code> <code>toString</code> <em>is</em> implemented (it's inherited from <code>AbstractCollection</code>), in Java 1.6:</p> <pre><code>public String toString() { Iterator&lt;E&gt; i = iterator(); if (! i.hasNext()) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (;;) { E e = i.next(); sb.append(e == this ? "(this Collection)" : e); if (! i.hasNext()) return sb.append(']').toString(); sb.append(", "); } } </code></pre> <p>It exits the loop early to avoid the comma after the last item. BTW: this is the first time I recall seeing "(this Collection)"; here's code to provoke it:</p> <pre><code>List l = new LinkedList(); l.add(l); System.out.println(l); </code></pre> <p>I welcome any solution, even if they use unexpected libraries (regexp?); and also solutions in languages other than Java (e.g. I think Python/Ruby have an <em>intersperse</em> function - how is <em>that</em> implemented?).</p> <p><em>Clarification</em>: by libraries, I mean the standard Java libraries. For other libraries, I consider them with other languages, and interested to know how they're implemented.</p> <p><em>EDIT</em> toolkit mentioned a similar question: <a href="https://stackoverflow.com/questions/285523/last-iteration-of-for-loop-in-java">Last iteration of enhanced for loop in java</a></p> <p>And another: <a href="https://stackoverflow.com/questions/156650/does-the-last-element-in-a-loop-deserve-a-separate-treatment">Does the last element in a loop deserve a separate treatment?</a></p>
 

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