Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a method with (int[] []) mean?

Tags:

java

We are given some code snippets to look at and figure out what the code does/will do.

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?

like image 790
James Avatar asked Dec 02 '25 23:12

James


1 Answers

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.

Taking your example 2D array:

int[][] in = {{2, 0, 2}, {3, 1, 2}, {1, 8, 4}};

This array has the following properties:

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;

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)

The first index a chooses the subarray, and the index b chooses the element inside the subarray.

like image 156
Tim Biegeleisen Avatar answered Dec 07 '25 04:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!