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);
}
,
only when the iterator has a next elementExample:
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 elementhasNext()
will always be trueNote: It's good to use StringBuilder for things like these. :)
With Java8 you can use String.Join()
with ArrayList
of Object
s.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With