欢马劈雪     最近更新时间:2020-01-02 10:19:05

500
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
二维数组并嵌套用于循环

public class ArrayOfArraysDemo2 {

    public static void main(String[] args) {

        int[][] aMatrix = new int[4][];/*n o w  j a v a  . c o m 提 供*/


        //populate matrix

        for (int i = 0; i < aMatrix.length; i++) {

            aMatrix[i] = new int[5]; //create sub-array

            for (int j = 0; j < aMatrix[i].length; j++) {

                aMatrix[i][j] = i + j;

            }

        }


        //print matrix

        for (int i = 0; i < aMatrix.length; i++) {/**nowjava - 时代Java**/

            for (int j = 0; j < aMatrix[i].length; j++) {

                System.out.print(aMatrix[i][j] + " ");

            }

            System.out.println();

        }

    }

}


展开阅读全文