【java学习笔记7】一维数组、二维数组的循环遍历

一维数组遍历:

package array;

public class ArrayTest01 {

    public static void main(String[] args) {

        // 静态初始化一维数组
        int[] array1 = {1,2,3,4,5};

        for (int i = 0;i < array1.length;i ++){
            System.out.println(array1[i]);
        }

        // 动态初始化一维数组
        int[] array2 = new int[5];
        for (int j = 0;j < array2.length;j ++){
            System.out.println(array2[j]);
        }

    }

}

 

二维数组遍历:

package array;

public class ArrayTest04 {

    public static void main(String[] args) {


        String[][] array = {
                {"java","python","c++"},
                {"cat","dog","pig"},
                {"Math","English","History"}
        };

        // 遍历二维数组
        for (int i = 0; i < array.length; i++) {
            for (int j = 0;j < array[i].length;j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}

执行结果是:

java python c++ 
cat dog pig 
Math English History 

 

posted @ 2020-12-28 08:57  愚人李愚  阅读(264)  评论(0编辑  收藏  举报