Skip to content
Advertisement

Print out elements from an Array with a Comma between the elements

I am printing out elements from an ArrayList and want to have a comma after each word except the last word.

Right now, I am doing it like this:

JavaScript

It prints out the words like this:

JavaScript

The problem is the last comma. How do I solve it?

Advertisement

Answer

Print the first word on its own if it exists. Then print the pattern as comma first, then the next element.

JavaScript

With a List, you’re better off using an Iterator

JavaScript
Advertisement