关于系统方法Arrays.toString()方法的内部流程,我写了个自己的方法让你了解他
class MyArrays{ //新建一个类
String res = null;
public static String toString(int[] a) { //静态方法 将数组以字符串的形式返回
String res = "";
for(int i=0;i<a.length;i++) { //遍历整个数组
if(i==0) { //将数组各个部分转化为字符串
res +="["+a[i];
}else if(i==(a.length-1)) {
res += ","+a[a.length-1]+"]";
}else {
res += ","+a[i];
}
}
return res; //返回值
}
}
public class Work {
public static void main(String[] args) {
int[] a = {1,2,3,5};
System.out.println(MyArrays.toString(a)); //直接调用已创好的静态方法
}
}