1. Overview
1.概述
A multi-dimensional array in Java is an array comprising arrays of varying sizes as its elements. It’s also referred to as “an array of arrays” or “ragged array” or “jagged array”.
Java中的多维数组是一个由不同大小的数组作为其元素的数组。它也被称为 “数组的数组 “或 “锯齿状数组 “或 “锯齿状数组”。
In this quick tutorial, we’ll look more in-depth into defining and working with multi-dimensional arrays.
在这个快速教程中,我们将更深入地研究多维数组的定义和工作。
2. Creating Multi-Dimensional Array
2.创建多维数组
Let’s start by looking at ways in which we can create a multi-dimensional array:
让我们先来看看创建多维数组的方法。
2.1. The Shorthand-Form
2.1.速记形式
An easy way to define a multi-dimensional array would be:
定义多维数组的一个简单方法是。
int[][] multiDimensionalArr = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
Here, we’ve declared and initialized multiDimensionalArr in a single step.
这里,我们在一个步骤中声明并初始化了multiDimensionalArr。
2.2. Declaration and Then Initialization
2.2.声明和随后的初始化
We start by declaring a multi-dimensional array of size three:
我们首先声明了一个大小为3的多维数组。
int[][] multiDimensionalArr = new int[3][];
Here, we’ve omitted to specify the second dimension since it will vary.
在这里,我们已经省略了指定第二个维度,因为它将会变化。
Next, let’s go further by both declaring and initializing the respective elements within multiDimensionalArr:
接下来,让我们进一步在multiDimensionalArr中声明和初始化各个元素。
multiDimensionalArr[0] = new int[] {1, 2};
multiDimensionalArr[1] = new int[] {3, 4, 5};
multiDimensionalArr[2] = new int[] {6, 7, 8, 9};
We can also simply declare its elements without initializing them:
我们也可以简单地声明其元素而不对其进行初始化。
multiDimensionalArr[0] = new int[2];
multiDimensionalArr[1] = new int[3];
multiDimensionalArr[2] = new int[4];
These can then later be initialized, for example by using user inputs.
然后,这些都可以随后被初始化,例如通过使用用户输入。
We can also use the java.util.Arrays.fill method to initialize array elements:
我们也可以使用java.util.Arrays.fill方法来初始化数组元素。
void initialize2DArray(int[][] multiDimensionalArray) {
for (int[] array : multiDimensionalArray) {
Arrays.fill(array, 7);
}
}
All the elements in the arrays are initialized with the same value.
数组中的所有元素都以相同的值进行初始化。
3. Memory Representation
3.记忆表征
How will the memory representation of our multiDimensionalArr look like?
我们的multiDimensionalArr的内存表示将是什么样子?
As we know, an array in Java is nothing but an object, the elements of which could be either primitives or references. So, a two-dimensional array in Java can be thought of as an array of one-dimensional arrays.
我们知道,Java中的数组只不过是一个对象,其中的元素可以是基元,也可以是引用。因此,Java中的二维数组可以被认为是一个一维数组。
Our multiDimensionalArr in memory would look similar to:
我们的multiDimensionalArr在内存中会看起来类似于。
Clearly, multiDimensionalArr[0] is holding a reference to a single-dimensional array of size 2, multiDimensionalArr[1] holds a reference to another one-dimensional array of size 3 and so on.
显然,multiDimensionalArr[0]持有对一个大小为2的单维数组的引用,multiDimensionalArr[1]持有对另一个大小为3的单维数组的引用,以此类推。
This way Java makes it possible for us to define and use multi-dimensional arrays.
这样,Java使我们有可能定义和使用多维数组。
4. Iterating Over Elements
4.迭代元素
We can iterate a multi-dimensional array much like any other array in Java.
我们可以像Java中的其他数组一样迭代一个多维数组。
Let’s try iterating and initializing the multiDimensionalArr elements using user inputs:
让我们尝试使用用户输入来迭代和初始化multiDimensionalArr元素。
void initializeElements(int[][] multiDimensionalArr) {
Scanner sc = new Scanner(System.in);
for (int outer = 0; outer < multiDimensionalArr.length; outer++) {
for (int inner = 0; inner < multiDimensionalArr[outer].length; inner++) {
multiDimensionalArr[outer][inner] = sc.nextInt();
}
}
}
Here, multiDimensionalArr[outer].length is the length of an array at an index outer in multiDimensionalArr.
这里,multiDimensionalArr[outer].length是multiDimensionalArr中索引outer的数组的长度。
It helps us to ensure that we are looking for elements only within a valid range of each sub-array, thereby avoiding an ArrayIndexOutOfBoundException.
它帮助我们确保我们只在每个子数组的有效范围内寻找元素,从而避免出现ArrayIndexOutOfBoundException。
5. Printing Elements
5.印刷元素
What if we want to print the elements of our multi-dimensional array?
如果我们想打印我们的多维数组的元素怎么办?
One obvious way would be to use the iteration logic we’ve already covered. This involves iterating through each item within our multi-dimensional array, which itself is an array, and then iterating over that child array – one element at a time.
一个明显的方法是使用我们已经介绍过的迭代逻辑。这涉及到在我们的多维数组中迭代每个项目,而多维数组本身就是一个数组,然后迭代子数组–一次一个元素。
Another option we have is to use java.util.Arrays.toString() helper method:
我们的另一个选择是使用java.util.Arrays.toString()辅助方法。
void printElements(int[][] multiDimensionalArr) {
for (int index = 0; index < multiDimensionalArr.length; index++) {
System.out.println(Arrays.toString(multiDimensionalArr[index]));
}
}
And we end up having clean and simple code. The generated console output would look like:
而我们最终得到的是干净而简单的代码。生成的控制台输出将看起来像。
[1, 2] [3, 4, 5] [6, 7, 8, 9]
6. Length of Elements
6.元素的长度
We can find the length of the arrays in a multi-dimensional array by iterating over the main array:
我们可以通过迭代主数组来找到多维数组中的数组长度。
int[] findLengthOfElements(int[][] multiDimensionalArray) {
int[] arrayOfLengths = new int[multiDimensionalArray.length];
for (int i = 0; i < multiDimensionalArray.length; i++) {
arrayOfLengths[i] = multiDimensionalArray[i].length;
}
return arrayOfLengths;
}
We can also find the length of arrays using Java streams:
我们也可以用Java流来查找数组的长度。
Integer[] findLengthOfArrays(int[][] multiDimensionalArray) {
return Arrays.stream(multiDimensionalArray)
.map(array -> array.length)
.toArray(Integer[]::new);
}
7. Copy a 2-D Array
7.复制一个二维阵列
We can copy a 2-D array using the Arrays.copyOf method:
我们可以使用Arrays.copyOf方法复制一个二维数组。
int[][] copy2DArray(int[][] arrayOfArrays) {
int[][] copied2DArray = new int[arrayOfArrays.length][];
for (int i = 0; i < arrayOfArrays.length; i++) {
int[] array = arrayOfArrays[i];
copied2DArray[i] = Arrays.copyOf(array, array.length);
}
return copied2DArray;
}
We can also achieve this by using Java streams:
我们也可以通过使用Java流来实现这一点。
Integer[][] copy2DArray(Integer[][] arrayOfArrays) {
return Arrays.stream(arrayOfArrays)
.map(array -> Arrays.copyOf(array, array.length))
.toArray(Integer[][]::new);
}
8. Conclusion
8.结语
In this article, we looked at what multi-dimensional arrays are, how they look in-memory and the ways in which we can define and use them.
在这篇文章中,我们看了什么是多维数组,它们在内存中的样子以及我们可以定义和使用它们的方式。
As always, the source code of the examples presented can be found over on GitHub.
像往常一样,所介绍的例子的源代码可以在GitHub上找到超过。