Java 中的多维数组

Java 中的多维数组不过是数组的数组(多个维度)。前一篇文章讨论了二维数组,它是多维数组最简单的形式。在此编程中,我们可以通过放置 n 个方括号 [ ] 来声明二维、n 维或多维数组,其中 n 是维度数。例如:

int[2][3][4] StudentArr = 三维数组

int[2][2][3][4] StudentArr = 四维数组

本文将展示如何在 Java 中声明和初始化多维数组。为了更好地理解多维数组,我们将使用三维数组。此外,您可以使用相同的技术尝试四维数组。

Java 中多维数组的声明

它展示了此编程中多维数组的声明。

Data_Type[][][] Arr_Name;

同样,我们可以声明其他类型的多维数组

int [][][] anIntegerType;
byte[][][] aByteType;
short[][][] aShortType;
long[][][] aLongType;
float[][][] aFloatType;
double[][][] aDoubleType;
boolean[][][] aBooleanTypeArr; 
char[][][] aCharArr;
String[][][] aStringArr;

在 Java 中创建多维数组

为了在 Java 编程语言中创建多维数组,我们必须使用 New 运算符。

Data_Type[][][] Name = new int[Tables][Row_Size][Column_Size];

如果您观察此多维数组的以上代码片段,

  • 表:它可以接受的表的总数。二维数组始终是具有行和列的单个表。相反,多维数组是具有多于一个表的行和列。
  • 行大小:行元素的数量。例如,行大小 = 5,则三维数组包含五行。
  • 列大小:它可以存储的列元素。列大小 = 6,则三维数组包含 6 列。

如果您已经初始化了一个 Java 多维数组,那么

double [][][] anEmployeeArr; // Declaration of Multidimensional array

// Crating an Java Multi dimensional Array
anEmployeeArr = new int[2][5][3];

例如:

int [][][] Employees = new int[2][5][3];

  1. 在这里,我们使用 int 作为数据类型来声明一个数组。这意味着以上数组将只接受双精度值,如果您尝试添加浮点数或双精度值,它将抛出错误。
  2. Employees 是 Java 多维数组的名称
  3. 表的数量 = 2。因此,此多维数组将最多容纳 2 层数据(行和列)。
  4. 行大小为 5,这意味着 Employees 只接受五个整数值作为行。
    • 如果我们尝试存储超过五个值,它将抛出错误。
    • 我们可以存储少于 5 个值。例如,如果存储三个整数值,则其余两个值将初始化为默认值(即 0)。
  5. 列大小为三;这意味着 Employees 将只接受三个整数值。
    • 如果我们尝试存储超过三个值,它将抛出错误。
    • 我们可以存储少于 3 个值。例如,如果存储一个整数值,则其余两个值将初始化为默认值(即 0)。
  6. 最后,Employees 最多可以容纳 24 个整数值(2 * 4 * 3 = 24)。

Java 中多维数组的初始化

我们可以通过多种方式初始化多维数组。

第一种方法

声明和创建多维数组。

int[][][] Student_Marks = new int[3][5][4];

以更传统的方式初始化元素

Student_Marks[0][0][0] = 15; // Initializing elements at position [0][0][0]
Student_Marks[1][1][0] = 45; // Initializing elements at position [1][1][0]
Student_Marks[2][0][1] = 65; // Initializing elements at position [2][0][1]

Java 多维数组第二种方法

int[][][] EmployeeArr = { { {10, 20, 30}, {50, 60, 70}, {80, 90, 100}, {110, 120, 130} },
                           { {15, 25, 35}, {22, 44, 66}, {33, 55, 77}, {78, 57, 76} }
                         };

我们没有提及数据级别、行大小和列大小。然而,编译器足够智能,可以通过检查行和列中的元素数量来计算大小。请参阅 数组二维 文章。

第三种方法

上述两种初始化 Java 多维数组的方法适用于在 3D 或 4D 中存储少量元素。如果我们要存储 100 行或 50 列值在 Java 中怎么办?使用前面提到的任何方法添加所有这些值将是一场噩梦。为了解决这个问题,我们可以在这里使用 Java 中的嵌套 For 循环 概念。

int tables, rows, columns;		
for(tables = 0; tables < 2; tables++) {
	for(rows = 0; rows < 3; rows++) {
		for(columns = 0; columns < 4; columns++) {
			EmployeeArr[tables][rows][columns] = tables + rows + columns; 
		}
	}			
}

提示:要存储/加载多维数组中的元素,您可以使用 For 循环While 循环Do While 循环

第四种方法

int[][][] Employees = new int[2][5][3];
Employees[0][0][0] = 102;
Employees[0][0][1] = 202;
Employees[0][0][2] = 305;

在这里,我们声明了一个大小为 2 层 * 5 行 * 3 列的三维数组,但我们只为一行分配了值。在这种情况下,其余值将是默认值(此处为 0)。

访问 Java 多维数组元素

在此编程语言中,我们可以使用索引位置来访问多维数组项。使用索引,我们可以访问或修改多维数组中存在的每个单独元素。

多维数组的索引值从 0 开始。它以 n-1 结束,其中 n 是表、行或列的大小。例如,int[][][] multiarr= new int[2][6][4] 允许存储最多两层数据(行和列)、6 个行元素和 4 个列元素。要访问或修改第一个值,请使用 multiarr[0][0][0];要访问或修改第一层中第二行第三列,请使用 multiarr[0][1][2]。要获取第二层中第六行第四列,请使用 multiarr[1][5][3]。

public class Sample {
	public static void main(String[] args) {
		int[][][] Student = { { {10, 20, 30}, {50, 60, 70}, {80, 90, 100}, {110, 120, 130} },
                                   { {15, 25, 35}, {22, 44, 66}, {33, 55, 77}, {78, 57, 76} }
                                  };
		System.out.println("Element at Student[0][0][0] = " + Student[0][0][0]);
		System.out.println("Element at Student[0][0][1] = " + Student[0][0][1]);
		System.out.println("Element at Student[0][0][2] = " + Student[0][0][2]);
		System.out.println("Element at Student[0][1][0] = " + Student[0][1][0]);
		System.out.println("Element at Student[0][1][1] = " + Student[0][1][1]);
		System.out.println("Element at Student[0][1][2] = " + Student[0][1][2]);
		System.out.println("Element at Student[0][2][0] = " + Student[0][2][0]);
		System.out.println("Element at Student[0][2][1] = " + Student[0][2][1]);
		System.out.println("Element at Student[0][2][2] = " + Student[0][2][2]);
		System.out.println("Element at Student[0][3][0] = " + Student[0][3][0]);
		System.out.println("Element at Student[0][3][1] = " + Student[0][3][1]);
		System.out.println("Element at Student[0][3][2] = " + Student[0][3][2]);
		
		//Accessing Second Table Rows & Columns
		System.out.println("=============");
		System.out.println("Element at Student[1][0][0] = " + Student[1][0][0]);
		System.out.println("Element at Student[1][0][1] = " + Student[1][0][1]);
		System.out.println("Element at Student[1][0][2] = " + Student[1][0][2]);
		System.out.println("Element at Student[1][1][0] = " + Student[1][1][0]);
		System.out.println("Element at Student[1][1][1] = " + Student[1][1][1]);
		System.out.println("Element at Student[1][1][2] = " + Student[1][1][2]);
		System.out.println("Element at Student[1][2][0] = " + Student[1][2][0]);
		System.out.println("Element at Student[1][2][1] = " + Student[1][2][1]);
		System.out.println("Element at Student[1][2][2] = " + Student[1][2][2]);
		System.out.println("Element at Student[1][3][0] = " + Student[1][3][0]);
		System.out.println("Element at Student[1][3][1] = " + Student[1][3][1]);
		System.out.println("Element at Student[1][3][2] = " + Student[1][3][2]);
	}
}

多维数组输出

Element at Student[0][0][0] = 10
Element at Student[0][0][1] = 20
Element at Student[0][0][2] = 30
Element at Student[0][1][0] = 50
Element at Student[0][1][1] = 60
Element at Student[0][1][2] = 70
Element at Student[0][2][0] = 80
Element at Student[0][2][1] = 90
Element at Student[0][2][2] = 100
Element at Student[0][3][0] = 110
Element at Student[0][3][1] = 120
Element at Student[0][3][2] = 130
=============
Element at Student[1][0][0] = 15
Element at Student[1][0][1] = 25
Element at Student[1][0][2] = 35
Element at Student[1][1][0] = 22
Element at Student[1][1][1] = 44
Element at Student[1][1][2] = 66
Element at Student[1][2][0] = 33
Element at Student[1][2][1] = 55
Element at Student[1][2][2] = 77
Element at Student[1][3][0] = 78
Element at Student[1][3][1] = 57
Element at Student[1][3][2] = 76

为了处理大量的行和列,我们必须使用 For 循环。让我们使用 For 循环访问上面的数组 Student[2][4][3]。

int tables, rows, columns;

for (tables = 0; tables < 2; tables++) {
     for (rows = 0; rows < 4; rows++) {
          for (columns = 0; columns < 3; columns++) {
	       System.out.format("%d", Student[tables][rows][columns]);
          }
     }
}

Java 多维数组示例

在此程序中,我们将声明 2 个多维数组并用一些值初始化它们。然后我们声明了另一个多维数组来保存和。

// Example 

public class AccessTwoDimentionalArray {
	public static void main(String[] args) {
		int[][][] a = { { {2, 4, 6, 8}, {12, 14, 16, 18}, {22, 24, 26, 28} }, 
                        { {32, 34, 36, 38}, {52, 54, 56, 58}, {72, 74, 76, 78} } };
		int[][][] b = { { {10, 20, 30, 40}, {50, 60, 70, 80}, {100, 110, 120, 140} }, 
                        { {150, 160, 170, 180}, {190, 200, 220, 240}, {250, 270, 290, 300} }
                      };
		int[][][] Sum = new int[2][3][4];
		int tables, rows, columns;		
		for(tables = 0; tables < a.length; tables++) {
			for(rows = 0; rows < a[0].length; rows++) {
				for(columns = 0; columns <= a[1].length; columns++) {
					Sum[tables][rows][columns] = a[tables][rows][columns] + b[tables][rows][columns]; 
				}
			}			
		}
		System.out.println("Sum Of those Two Arrays are: ");
		for(tables = 0; tables < a.length; tables++) {
			for(rows = 0; rows < a[0].length; rows++) {
				for(columns = 0; columns <= a[1].length; columns++) {
					System.out.format("%d \t", Sum[tables][rows][columns]);
				}
				System.out.println("");
			}
			System.out.println("");
		}
	}
}
Multi Dimensional Array in Java 2

在此多维数组示例中,首先,我们声明了两个大小为 [2],[3] 的三维数组 a 和 b,并用一些随机值初始化它们。我们还声明了一个大小为 [2],[3] 的空数组

下面的 For 循环 将有助于遍历 a 和 b 中存在的每个单元格。For 循环内部的条件 (rows < a[0].length) 将确保编译器不会超出数组行限制,而 (columns < a[1].length) 将确保编译器不会超出数组列限制。

提示:a.length 用于查找数据级别(第一维),a[0].length 用于查找行长度(第二维),a[1].length 用于查找列长度(第三维)。

for(tables = 0; tables < a.length; tables++) {
	for(rows = 0; rows < a[0].length; rows++) {
		for(columns = 0; columns <= a[1].length; columns++) {
			Sum[tables][rows][columns] = a[tables][rows][columns] + b[tables][rows][columns]; 
		}
	}			
}

分析

让我们看看 Java 多维数组程序的迭代方式

表第一次迭代

表的数量 = 0,条件 (tables < 2) 为 True。因此,它将进入第二个 for 循环(行迭代)。

行第一次迭代

行值 = 0,条件 (rows < 2) 为 True。因此,它进入第二个 for 循环。

列第一次迭代

列值 = 0,条件 (columns < 3) 为 True。因此,它将开始执行循环内的语句,直到条件失败。

  • Sum[tables][rows][columns] = a[tables][rows][columns] + b[tables][rows][columns];
  • Sum[0][0][0] = a[0][0][0] + b[0][0][0] = 2 + 10 = 12;

列值将增加 1。

第二次迭代:列值 = 1,条件 (columns <= 3) 为 True。由于我们没有退出内部循环(列循环),所以行值仍然是 0。

  • Sum[0][0][1]= a[0][0][1] + b[0][0][1] = 4 + 20;
  • Sum[0][0][1]= 24;

列将增加 1。

第三次迭代:列值为 2,条件 (columns <= 3) 为 True。由于我们没有退出内部循环(列循环),所以行值将为 0。

  • Sum[0][0][2] = a[0][0][2] + b[0][0][2] = 6 + 30;
  • Sum[0][0][2] = 36;

列增加 1。

第四次迭代:列值 = 3,条件 (columns <= 3) 为 True。由于我们没有退出内部循环(列循环),所以行值将为 0。

Sum[0][0][3] = a[0][0][2] + b[0][0][2] = 8 + 40;

Sum[0][0][3] = 48;

从第四次迭代开始,列的值变为 4,条件 (columns <= 3) 将失败。因此它将退出循环。现在行值将增加 1 并开始第二次行迭代。

Java 多维数组 – 行第二次迭代

行值将为 1,条件 (rows < 2) 为 True。因此,它将进入第二个 for 循环

列第一次迭代

列值 = 0,条件 (columns <= 3) 为 True。因此,它将开始执行循环内的语句,直到条件失败。

  • Sum[0][1][0] = a[0][1][0] + b[0][1][0];
  • Sum[0][1][0] = 12 + 50 = 62;

列增加 1。

第二次迭代:列值 = 1,条件 (columns <= 3) 为 True。

  • Sum[0][1][1]= a[0][1][1] + b[0][1][1] = 14 + 60;
  • Sum[0][1][1]= 74;

列值将增加 1。

第三次迭代:列值 = 2,条件 (columns <= 3) 为 True。

  • Sum[0][1][2] = a[0][1][2] + b[0][1][2] = 16 + 70;
  • Sum[0][1][2] = 86;

第四次迭代:列值 = 3,条件 (columns <= 3) 为 True。

  • Sum[0][1][3] = a[0][1][2] + b[0][1][2] = 18 + 80;
  • Sum[0][1][3] = 98;

递增后,列值将变为四,条件 (columns <= 3) 将失败。因此它将退出列 For 循环。接下来,行值将增加 1。这意味着 rows = 2。条件 (rows < 2) 将失败。因此,它将退出行循环。

现在,表的数量将增加 1。这意味着表的数量 = 1,条件 (tables < 2) 为 TRUE。因此,它将开始执行表的第二次迭代。重复上述过程一次以获取剩余结果。

Java 多维数组中的下一个 for 循环将按照上述解释遍历。但是,它不会求和,而是使用其中的 system.out.format 语句以制表符分隔的方式单独显示值。