方法一:
package com.codyy.sso.controller.yuanqu;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
StringBuilder str = new StringBuilder();
List<String> list = new ArrayList<String>();
list.add("你");
list.add("好");
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1)// 当循环到最后一个的时候 就不添加逗号,
{
str.append(list.get(i));
} else {
str.append(list.get(i));
str.append(",");
}
}
System.out.println(str);
}
}
方法二:
package com.codyy.sso.controller.yuanqu;
public class Test {
public static void main(String[] args) {
StringBuilder str=new StringBuilder();
for (int i = 0; i < 4; i++) {
str.append(i+"1,");
}
String ss=str.substring(0,str.length()-1);//当循环结束后截取最后一个逗号
System.out.println(ss);
}
}