迴圈拼接List並以逗號間隔
阿新 • • 發佈:2018-11-29
package list; import java.util.ArrayList; import java.util.List; public class list { public static void main(String[] args) { List<String> resultList = new ArrayList<String>(); resultList.add("a1"); resultList.add("a2"); resultList.add("a3"); resultList.add("a4"); StringBuffer resultBuffer = new StringBuffer(); for (int i = 0; i < resultList.size(); i++) { String result = resultList.get(i); if (i == 0) { resultBuffer.append(result); } else { resultBuffer.append("," + result); } } String testResult = resultBuffer.toString(); System.out.println(testResult); } }