求助大神:java中现有一个list要取出list中所有元素拼接成字符串以逗号隔开应该如何做?

2024-11-17 04:55:59
推荐回答(2个)
回答1:

1、java">public class Test {

public static void main(String[] args) {

List list = new ArrayList<>();

list.add("a");

list.add("b");

list.add("c");

list.add("d");

String str = "";

for(int i=0;i

str += list.get(i)+",";

}

System.out.println(str);

回答2:

java8里面有自带的拼接操作:

List list = new ArrayList<>();
list.add("ddd");
list.add("eee");
list.add("fff");
String ret = list.stream().collect(Collectors.joining(","));