编写多维数组使用方法的程序如下:
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] + "\n"); sum += a[i][j][k]; } } } System.out.println("sum = " + sum); }}
运行结果:
a[0][0][0] = 5a[0][0][1] = 1a[0][1][0] = 6a[0][1][1] = 7a[1][0][0] = 9a[1][0][1] = 4a[1][1][0] = 8a[1][1][1] = 3sum = 43