字符串数组转换为字符串并以符号拼接

String.join方式

List list = new ArrayList<>(Arrays.asList("1","2","3","4","5"));
String result = String.join(",",list);
System.out.println(result);

Stream流

List list = new ArrayList<>(Arrays.asList("1","2","3","4","5"));        
String result2 = list.stream().collect(Collectors.joining(","));
System.out.println(result2);

StringBuffer

StringBuffer sb = new StringBuffer();
for(String str : list){
   sb.append(str).append(",");
}
String result3 = sb.toString().substring(0,sb.length()-1);
System.out.println(result3);

for遍历

List list = new ArrayList<>(Arrays.asList("1","2","3","4","5"));
String result4 = "";
for(int i = 0;i < list.size();i++){
     if(i == list.size() - 1){
           result4 += list.get(i);
      }else{
            result4 += list.get(i) + ",";
           }
      }
System.out.println(result4);

你可能感兴趣的:(Java,java)