(int [] [])的方法是什么意思? [英] What does a method with (int[] []) mean?
问题描述
我们给出了一些代码片段来查看并弄清楚代码会做什么/将要做什么。
We are given some code snippets to look at and figure out what the code does/will do.
我理解使用数组的方法和方法但我从未见过 methodName(int [] [] m)
有两个 [] []
I understand methods and methods with arrays but I have never seen methodName(int[][] m)
with two [][]
这是什么意思?数组中的数组?
What does this mean? an array within an array?
推荐答案
int [] []
in方法签名是指双整数数组。您可以将双整数数组视为 int
值的矩阵。
int[][]
in the method signature refers to a double array of integers. You can think of a double integer array as being a matrix of int
values.
以您的示例2D数组为例:
Taking your example 2D array:
int[][] in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};
此数组具有以下属性:
System.out.println(in.length); // prints 3 (number of arrays inside 'in')
System.out.println(in[0].length); // prints 3 (number of ints in first array)
System.out.println(in[1].length); // also prints 3 (number of ints in second array)
这是一个视觉效果,向您展示如何访问此数组有效:
Here is a visual to show you how accessing this array works:
int a = 1;
int b = 0;
然后在[a] [b] ==中的 [1] [ 0] == 3
:
Then in[a][b] == in[1][0] == 3
:
2 0 2
{3 1 2} <-- a = 1 (second subarray)
1 8 4
{3 1 2}
^-- b = 0 (first element in that subarray)
第一个索引 a
选择子阵列,索引 b
选择子数组中的元素。
The first index a
chooses the subarray, and the index b
chooses the element inside the subarray.
这篇关于(int [] [])的方法是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!