Print out elements from an array with a comma betw

2019-01-07 21:07发布

I am printing out elements from a array list, I want to have a comma between each word except the last word. Right now I am doing like this:

    for (String s : arrayListWords) {
         System.out.print(s + ", ");
    }

As you understand it will print out the words like this: "one, two, three, four," and the problem is the last comma, how do I solve this? All answers appreciated!

Best regards, Erica

12条回答
2楼-- · 2019-01-07 21:48

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

if (arrayListWords.length >= 1) {
    System.out.print(arrayListWords[0]);
}

// note that i starts at 1, since we already printed the element at index 0
for (int i = 1; i < arrayListWords.length, i++) { 
     System.out.print(", " + arrayListWords[i]);
}

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

// assume String
Iterator<String> it = arrayListWords.iterator();
if (it.hasNext()) {
    System.out.print(it.next());
}
while (it.hasNext()) {
    System.out.print(", " + it.next());
}
查看更多
可以哭但决不认输i
3楼-- · 2019-01-07 21:48
StringJoiner str = new StringJoiner(", ");
str.add("Aplha").add("Beta").add("Gamma");

String result = str.toString();
System.out.println("The result is: " + result);

The output: The result is: Aplha, Beta, Gamma

查看更多
贪生不怕死
4楼-- · 2019-01-07 21:48

You could use a standard function in the java.util package and remove the block quotes at start and end.

String str = java.util.Arrays.toString(arrayListWords);
str = str.substring(1,str.length()-1);
System.out.println(str);
查看更多
该账号已被封号
5楼-- · 2019-01-07 21:51

I would write it this way:

String separator = "";  // separator here is your ","

for (String s : arrayListWords) {
    System.out.print(separator + s);
    separator = ",";
}

If arrayListWords has two words, it should print out A,B

查看更多
我命由我不由天
6楼-- · 2019-01-07 21:53

You can use an Iterator on the List to check whether there are more elements.

You can then append the comma only if the current element is not the last element.

public static void main(String[] args) throws Exception {
    final List<String> words = Arrays.asList(new String[]{"one", "two", "three", "four"});

    final Iterator<String> wordIter = words.iterator();
    final StringBuilder out = new StringBuilder();
    while (wordIter.hasNext()) {
        out.append(wordIter.next());
        if (wordIter.hasNext()) {
            out.append(",");
        }
    }
    System.out.println(out.toString());
}

However it is much easier to use a 3rd party library like Guava to do this for you. The code then becomes:

public static void main(String[] args) throws Exception {
    final List<String> words = Arrays.asList(new String[]{"one", "two", "three", "four"});
    System.out.println(Joiner.on(",").join(words));
}
查看更多
Ridiculous、
7楼-- · 2019-01-07 21:53

With Java 8 it got much easier, no need for 3rd parties -

final List<String> words = Arrays.asList("one", "two", "three", "four");
String wordsAsString = words.stream().reduce((w1, w2) -> w1 + "," + w2).get();
System.out.println(wordsAsString);
查看更多
登录 后发表回答