/*
Arrays toString
*/
public class ArraysDemo4 {
public static void main(String[] args) {
int[] arr = {1,245,657,3,456,8};
//arr = null;
String string = Arrays.toString(arr);
System.out.println(string);
}
public static String toString(int[] a) {
if (a == null) //非空校验
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');//"["
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax){
return b.append(']').toString();
}
b.append(", ");
}
}
}
|
|