方法一:直接使用StringBuffer进行转换,简单易懂
public String arrayToString() {
List<Character> result = new ArrayList<Character>();
result.add('a');
result.add('b'); //['a', 'b']
StringBuffer resultStr = new StringBuffer();// 利用StringBuffer将arraylist转为string
for (int i = 0; i < result.size(); i++) {
resultStr.append(result.get(i));
}
return resultStr.toString();
}
方法二:使用org.apache.commons.lang包下的StringUtils类的join方法,面试中不推荐使用
public String arrayToString() {
List<Character> result = new ArrayList<Character>();
result.add('a');
result.add('b'); //['a', 'b']
String resultStr = StringUtils.join(result, ",");
return resultStr;
}
版权声明:本文为TylerDu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。