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:57

Here is what I come up with:

String join = "";

// solution 1
List<String> 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);

off course we can utalise StringBuilder but of all solutions, I like @Mav answer as it's more efficient and clean.

查看更多
祖国的老花朵
3楼-- · 2019-01-07 22:07

Just use the toString() method.

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.
查看更多
smile是对你的礼貌
4楼-- · 2019-01-07 22:10

Using Java 8 Streams:

Stream.of(arrayListWords).collect(Collectors.joining(", "));
查看更多
来,给爷笑一个
5楼-- · 2019-01-07 22:13

While iterating, you can append the String s to the StringBuilder and at the end, you can delete the last 2 chars which is an extra , and a space (res.length() -2)

StringBuilder res = new StringBuilder();
for (String s : arrayListWords) {
    res.append(s).append(", ");
}
System.out.println(res.deleteCharAt(res.length()-2).toString());
查看更多
做个烂人
6楼-- · 2019-01-07 22:13

You can try this

    List<String> listWords= Arrays.asList(arrayListWords); // convert array to List
    StringBuilder sb=new StringBuilder();
    sb.append(listWords);
    System.out.println(sb.toString().replaceAll("\\[|\\]",""));
查看更多
贼婆χ
7楼-- · 2019-01-07 22:13

This might be the most efficient means of comma-delimited string using best practices and no "if" checks, no unfamiliar libraries, and StringBuilder which is best practice for concatenating strings.

Also having a "size" variable reduces the calls to the .size() method.

For those using String[]:

StringBuilder str = new StringBuilder();
String[] strArr = {"one", "two", "three", "four"};
int size = strArr.length;
str.append(strArr[0]);
for (int i = 1; i < size; i++) {
    str.append(",").append(strArr[i]);
}
System.out.println(str.toString());

For those using ArrayList<String>:

StringBuilder str = new StringBuilder();
List<String> strArr = Arrays.asList(new String[]{"one", "two", "three", "four"});
int size = strArr.size();
str.append(strArr.get(0));
for (int i = 1; i < size; i++) {
    str.append(",").append(strArr.get(i));
}
System.out.println(str.toString());

Both yield: one,two,three,four

查看更多
登录 后发表回答