将String类型的字符串拼接成以逗号分隔的字符输出
package test; import java.util.ArrayList; import java.util.List; public class Tesdssss { private static List<String> dataList = new ArrayList<String>(); public static void main(String[] args) { initList(); dataToString(); } private static void dataToString() { StringBuilder sb = new StringBuilder(); for(int i = 0; i < dataList.size(); i++) { if (sb.length() > 0) {//该步即不会第一位有逗号,也防止最后一位拼接逗号! sb.append(","); } sb.append(dataList.get(i)); } System.out.println(sb.toString()); } private static void initList() { for(int i =0; i < 6; i++) { dataList.add("第"+i+"个字符串"); } } }