写一个方法用来遍历打印所有数组元素,打印格式如下 [10,20,30,40,50]

package 课堂代码;

public class Test6 {
    //写一个方法用来遍历打印所有数组元素,打印格式如下
    //[10,20,30,40,50]
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        newArray(arr);
        System.out.println("-------------");
    }

    public static void newArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i != arr.length - 1) {
                System.out.print(arr[i] + ", ");
            } else {
                System.out.print(arr[i]);
            }
        }
        System.out.println("]");
    }

}

版权声明:本文为heima_hao123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。