/**
*@author:肖佳嘉
*
*/
* public static String toString(int[] a)
* public static void sort(int[] a)
* public static int binarySearch(int[] a,int key)
*
* public static String toString(int[] a) {
if (a == null) //如果传入的数组是null
return "null"; //返回null
int iMax = a.length - 1; //iMax最大索引
if (iMax == -1) //如果数组中没有元素
return "[]"; //返回[]
StringBuilder b = new StringBuilder(); //线程不安全,效率高
b.append('['); //将[添加到字符串缓冲区中
for (int i = 0; ; i++) { //遍历数组,判断语句没有写默认是true
b.append(a[i]); //把第一个元素添加进字符串缓冲区
if (i == iMax) //如果索引等于了最大索引值
return b.append(']').toString(); //将]添加到字符串缓冲区,在转换成字符串并返回
b.append(", "); //如果不等于最大索引就将, 添加到缓冲区
}
}