Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java foreach and hasNext

Tags:

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);
}
like image 699
Alter Avatar asked Mar 16 '23 13:03

Alter


2 Answers

  • 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. :)

like image 67
almightyGOSU Avatar answered Mar 23 '23 01:03

almightyGOSU


Update

With Java8 you can use String.Join() with ArrayList of Objects.

public static void main(String[] args) throws Exception {
    ArrayList<MyClass> test = new ArrayList();
    test.add(new MyClass("Adam", 12));
    test.add(new MyClass("Bob", 17));
    test.add(new MyClass("Catherine", 19));
    test.add(new MyClass("Dylan", 22));

    System.out.println(String.join(", ", test.stream().map(mc -> mc.Name).collect(Collectors.toList())));
}

public static class MyClass {
    public String Name;
    public int Age;

    public MyClass(String n, int a) {
        this.Name = n;
        this.Age = a;
    }
}

Results:

Adam, Bob, Catherine, Dylan

OLD ANSWER

Why don't you use String.Join()?

public static void main(String[] args) throws Exception {
    ArrayList<String> test = 
            new ArrayList<String>(Arrays.asList(
                    "Adam", "Bob", "Catherine", "Dylan"));

    System.out.println(String.join(", ", test));
}

Results:

Adam, Bob, Catherine, Dylan
like image 28
Shar1er80 Avatar answered Mar 22 '23 23:03

Shar1er80