title: Removing last comma or other delimiter when printing list to string
body: <p>Let's assume that I have a <code>datastructure</code> with <code>n</code> elements <code>(0, 1, ... , n-1)</code>. </p>
  
  <p>And assume these elements have some property I want to print (assume an <code>int</code>). And I want to print the <code>datastructure</code> on one line, with a delimiter (<code>comma</code>, <code>space</code>). So, the result would be something like this</p>
  
  <p><code>13, 14, 15, 16, 17</code></p>
  
  <p>What is the best way to add the comma and space (in Java)?</p>
  
  <p>These are approaches I've taken:</p>
  
  <ol>
  <li><p>If the <code>datastructure</code> is <code>Iterable</code>, iterate from <code>0 to n-2</code> and append the delimiter to each of those, then print the last element.</p></li>
  <li><p>Loop through all elements and append everything to empty <code>String</code> (or <code>Stringbuilder</code>), then at the very end remove the last deliminator (<code>hardcoded</code>). </p></li>
  </ol>
  
  <p>I feel like there's something better.</p>
  
  <p>Edit: The question is unique. The 'possible duplicate' involves only printing, not messing with delimiters. </p>
  
  <p>Edit2: Attempts as requested</p>
  
  <pre><code>// 1st approach
  public void printStuff(ArrayList&lt;Integer&gt; input) {
      for (int i=0; i&lt;input.size()-1; i++) {
          System.out.print(input.get(i) + ", ");
      }
      System.out.println(input.get(input.size()-1);
  }
  </code></pre>
  
  <p>Another attempt</p>
  
  <pre><code>// 2nd approach
  public void printStuff(ArrayList&lt;Integer&gt; input) {
      StringBuilder sb = new StringBuilder();
      for (int i=0; i&lt;input.size(); i++) {
          sb.append(input.get(i)+", ");
      }
      sb.substring(0, input.size()-2);
      System.out.println(sb.toString());
  }
  </code></pre>