I'm iterating through a list of list of strings. I need to print each list in comma separated format except the last element. This is my code.
for(Iterator itr = list.iterator(); itr.hasNext();){
List<String> l = (List<String>) itr.next();
for(Iterator it = l.iterator(); it.hasNext();){
System.out.print(it.next() + " ");
}
System.out.println();
}
This prints something like
listen, silent, tinsel,
How do I format the print logic to remove the comma after the last element. I can do that on for i
loop checking the value of i
against the last index, but wish to know how to do it with an Iterator
in Java.