Java foreach and hasNext

java

I'm trying to loop through an ArrayList of Objects. For each Object, I want to call its toString() method and then join them with commas separating them.

I have some test code, but it's not performing like I would expect. The output I get for the following code is: Adam, Bob, Catherine, Dylan, When what I want is Adam, Bob, Catherine, Dylan (the last name shouldn't have a comma succeeding it.)

public static void main2 (){
    //ArrayList of Strings rather than Objects for simplicity
    ArrayList<String> test = new ArrayList<String>(Arrays.asList("Adam", "Bob", "Catherine", "Dylan"));

    String output = new String();
    for (String str : test) {
        output += str.toString();
        output += test.iterator().hasNext() ? ", " : "";
    }

    System.out.println(output);
}

Best Answer

  • Just use the iterator alone, instead of the 'for-each' loop
  • Append a , only when the iterator has a next element

Example:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class QuickTester {

    public static void main(String[] args) {

        ArrayList<String> test = 
                new ArrayList<String>(Arrays.asList(
                        "Adam", "Bob", "Catherine", "Dylan"));

        StringBuilder sb = new StringBuilder();
        Iterator<String> stringIterator = test.iterator();
        while(stringIterator.hasNext()) {

            sb.append(stringIterator.next());

            if(stringIterator.hasNext()) {
                sb.append(", ");
            }
        }

        System.out.println(sb.toString());
    }
}

Output:

Adam, Bob, Catherine, Dylan


In the question, for the line

output += test.iterator().hasNext() ? ", " : "";
  • test.iterator() returns an Iterator that points to the first element
  • since there are 4 elements, hasNext() will always be true
  • that explains why the output is not as expected
  • Furthermore, an iterator alone is sufficient, no need to use both 'for-each' and iterator.

Note: It's good to use StringBuilder for things like these. :)