从数组中打印出元素,并在除最后一个单词之外的元素之间使用逗号

我打印出数组列表中的元素,我想在除了最后一个单词之外的每个单词之间使用逗号。 现在我这样做:

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

如你所知,它会打印出这样的字样:“一,二,三,四”,问题是最后一个逗号,我该如何解决? 所有答案赞赏!

最好的问候,埃里卡

如果存在,则单独打印第一个单词。 然后先将模式打印为逗号,然后打印下一个元素。

 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]); } 

使用List ,您最好使用Iterator

 // assume String Iterator it = arrayListWords.iterator(); if (it.hasNext()) { System.out.print(it.next()); } while (it.hasNext()) { System.out.print(", " + it.next()); } 

我会这样写:

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

如果arrayListWords有两个单词,它应该打印出A,B

使用Java 8 Streams:

 Stream.of(arrayListWords).collect(Collectors.joining(", ")); 

在迭代时,您可以将String s附加到StringBuilder ,最后,您可以删除最后2个额外的字符和空格( res.length() -2

 StringBuilder res = new StringBuilder(); for (String s : arrayListWords) { res.append(s).append(", "); } System.out.println(res.deleteCharAt(res.length()-2).toString()); 

您可以在java.util包中使用标准函数,并在开始和结束时删除块引号。

 String str = java.util.Arrays.toString(arrayListWords); str = str.substring(1,str.length()-1); System.out.println(str); 
 StringJoiner str = new StringJoiner(", "); str.add("Aplha").add("Beta").add("Gamma"); String result = str.toString(); System.out.println("The result is: " + result); 

输出:结果是:Aplha,Beta,Gamma

您可以使用List上的Iterator来检查是否有更多元素。

然后,只有当前元素不是最后一个元素时才可以附加逗号。

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

但是,使用像Guava这样的第三方库来为您完成此操作要容易得多。 代码然后变成:

 public static void main(String[] args) throws Exception { final List words = Arrays.asList(new String[]{"one", "two", "three", "four"}); System.out.println(Joiner.on(",").join(words)); } 

你可以试试这个

  List listWords= Arrays.asList(arrayListWords); // convert array to List StringBuilder sb=new StringBuilder(); sb.append(listWords); System.out.println(sb.toString().replaceAll("\\[|\\]","")); 

这是我想出的:

 String join = ""; // solution 1 List strList = Arrays.asList(new String[] {"1", "2", "3"}); for(String s: strList) { int idx = strList.indexOf(s); join += (idx == strList.size()-1) ? s : s + ","; } System.out.println(join); // solution 2 join = ""; for(String s: strList) { join += s + ","; } join = join.substring(0, join.length()-1); System.out.println(join); // solution 3 join = ""; int count = 0; for(String s: strList) { join += (count == strlist.size()-1) ? s: s + ","; count++; } System.out.println(join); 

当然我们可以使用StringBuilder而不是所有解决方案,我喜欢@Mav回答,因为它更有效和干净。

使用Java 8它变得更容易,不需要第三方 –

 final List words = Arrays.asList("one", "two", "three", "four"); String wordsAsString = words.stream().reduce((w1, w2) -> w1 + "," + w2).get(); System.out.println(wordsAsString); 

只需使用toString()方法。

 String s = arrayListWords.toString(); System.out.println(s); //This will print it like this: "[one, two, three, four]" //If you want to remove the brackets you can do so easily. Just change the way you print.