编写多维数组使用方法的程序如下:
package com.liaojianya.chapter1; /** * This program demonstrates the use of multiple array. * @author LIAO JIANYA * 2016年7月20日 */ public class ThreeDimensionArray { public static void main(String[] args) { int sum = 0; int[][][] a = { {{5, 1}, {6, 7}}, {{9, 4}, {8, 3}} }; for(int i = 0; i < a.length; i++) { for(int j = 0; j < a[i].length; j++) { for(int k = 0; k < a[j].length; k++) { System.out.print("a[" + i + "][" + j + "][" + k + "] = " + a[i][j][k] + " "); sum += a[i][j][k]; } } } System.out.println("sum = " + sum); } }
运行结果:
a[0][0][0] = 5 a[0][0][1] = 1 a[0][1][0] = 6 a[0][1][1] = 7 a[1][0][0] = 9 a[1][0][1] = 4 a[1][1][0] = 8 a[1][1][1] = 3 sum = 43