1 package string.demo; 2 3 /* 4 * 将数组变为字符串输出 5 */ 6 public class StringBuilderTest 7 { 8 9 /** 10 * @param args 11 */ 12 public static void main(String[] args) 13 { 14 int[] arr = {2, 3, 41, 4}; 15 String str = arrayToString_2(arr); 16 System.out.println(str); 17 18 } 19 20 private static String arrayToString_2(int[] arr) 21 { 22 StringBuilder sb = new StringBuilder("["); 23 for (int i = 0; i < arr.length; i++) 24 { 25 if (i != arr.length - 1) 26 sb.append(arr[i] + ", "); 27 else 28 sb.append(arr[i] + "]"); 29 } 30 return sb.toString(); 31 } 32 33 public static String arrayToString_1(int[] arr) 34 { 35 String str = "["; 36 for (int i = 0; i < arr.length; i++) 37 { 38 if (i != arr.length - 1) 39 str += arr[i] + ", "; 40 else 41 str += arr[i] + "]"; 42 } 43 return str; 44 } 45 }