打印数组的三种常用方法(自己亲手总结的)
//第一种
public static void print1(int[] arr) {
// TODO Auto-generated method stub
String s="[";
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
s+=arr+"]";
}else{
s+=arr+", ";
}
}
System.out.println(s);
}
// 第二种
public static void print2(int[] arr) {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("[");
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
sb.append(arr+"]");
}else{
sb.append(arr+", ");
}
}
System.out.println(sb);
}
//第三种
public static void print3(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i==arr.length-1) {
System.out.println((arr+"]"));
}else{
System.out.print((arr+", "));
}
}
}
欢迎各位初学者多多关注!QQ:451704940
打印结果是::::[0, 1, 2, 4, 5, 6]
|
|