4 回答
TA贡献1817条经验 获得超14个赞
您不需要StringUtils
默认情况下List
toString()
以逗号分隔格式显示元素。
System.out.println (new StringBuilder (aAndB.toString()) .deleteCharAt (aAndB.toString().length ()-1) .deleteCharAt (0).toString ());
您唯一需要做的就是删除方括号
TA贡献1911条经验 获得超7个赞
由于您使用的是 Java 7,因此您可以编写一个静态方法来执行该任务。
List<String> a = Arrays.asList("a", "b", "c");
List<String> b = Arrays.asList("d", "e", "f");
String s = join(",", a, b);
System.out.println(s);
List<Integer> aa = Arrays.asList(101, 102, 103);
List<Integer> bb = Arrays.asList(104, 105, 106);
String ss = join(":", aa, bb);
System.out.println(ss);
}
public static <T> String join(String delimiter, List<T>... lists) {
StringBuilder sb = new StringBuilder();
for (List<T> list : lists) {
for (T item : list) {
sb.append(delimiter);
sb.append(item);
}
}
return sb.substring(delimiter.length()).toString();
}
}
这打印。
a、b、c、d、e、f
101:102:103:104:105:106
TA贡献2065条经验 获得超13个赞
您可以像这样使用番石榴库:
String [] a = {"a", "b", "c"};
String [] b = {"d", "e"};
//using Guava library
String [] joined = ObjectArrays.concat(a, b, String.class);
System.out.println("Joined array : " + Arrays.toString(joined));
// Output: "Joined array : [a, b, c, d, e]"
添加回答
举报