public class Demo5_ArrayToString {
/**
*用StringBuffer方法将数组转换为字符串
*/
public static void main(String[] args) {
int[] arr = {1,2,3};
StringBuffer sb = new StringBuffer();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
sb.append(arr[i]).append("]");
}else{
sb.append(arr[i]+" ,");
}
}
System.out.println(sb);
}
} |
|