package demo; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ListDemo { public static void main(String[] args) { String str = "A,B"; // 字符串转List List<String> list = new ArrayList<>(Arrays.asList(str.split(","))); list.add("C"); // List转字符串 String line = String.join(",", list); System.out.println(line); // A,B,C } }
Arrays.asList 返回的List不能进行添加操作,需要用ArrayList包装一下
UnsupportedOperationException
参考