简体   繁体   中英

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.

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 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.

It represents multi dimensional arrays (AKA arrays or arrays) of given data type. Think hierarchical to understand it the best way. If you have int[3][2], it means, It holds value for each of the following index.

int[0][0]
int[0][1]
int[1][0]
int[1][1]
int[2][0]
int[2][1]

Hope it will help. I struggled a lot to understand it when i was a beginner. Possible assign is

int[3][2] iValue = {{00,01}, {10,11}, {20, 21}}

Thanks for the correction.

methodName(int[] []) is an array of arrays. In response to all the comments, I tested it in eclipse and the length is 3.

In many programming languages (including Java), it is possible to create (and use) an array of arrays. In Java (specifically), all arrays are Object instances. Consider

Object intArray1 = new int[10];
Object intArray2 = new int[10];
Object intArray3 = new int[10];

Then you might have

Object[] arrs = { intArray1, intArray2, intArray3 };

or even

Object arrs = new Object[] { intArray1, intArray2, intArray3 };

JLS-15.10.1 Run-Time Evaluation of Array Creation Expressions says (in part)

Otherwise, if n DimExpr expressions appear, then array creation effectively executes a set of nested loops of depth n-1 to create the implied arrays of arrays.

A multidimensional array need not have arrays of the same length at each level.

Finally, there is Arrays.deepToString(Object[]) the Javadoc says (in part)

Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

In Java, " int [ ][ ] " stands for a 2-dimensional integer array . To make it easy to understand, simply we can compare 2-d integer array with a simple 1-d integer array;

1) Down below, a 1-d int array is initialized;

int[] arr1d = { 1,2,3 };

2) And on this one, a 2-d int array is initialized;

int[][] arr2d = { {1,2,3}, {4,5,6} };

It is important to understand the structure of 2d arrays. If you print the length of the arr2d , you will get 2 the rows of the array which is 2.

System.out.println(arr2d[].length);

You will get the length of the outer array, which is actually row count of the array.

To get the inner array length, which is actually the column count;

System.out.println(arr2d[0].length);

Notice that we take the first row, and get the length of the inner array and print the column number.

To get familiar with the usage of the 2d array in a method, you can check this out;

private static void printIntegerArray(int[][] intArray) {
    for(int i = 0; i < intArray.length; i++ ) {
        for(int j = 0; j < intArray[i].length; j++ ) {
            System.out.printf("%3d ", intArray[i][j]);
        }
        System.out.println();
    }
}

In this static method, int[][] intArray is the only parameter which is obviously a 2 dimensional int array. There are two nested for loops to print the array as a matrix. The outer loop is traversing the rows and the inner loop is traversing on the inner loop.

Here is the complete example for the 2D Method usage;

public class Test2DArray {

public static void main(String[] args) {
    //Init 2d integer list
    int simpleArray[][] = { {1,2,3,4,5}, {6,7,8,9,10}, {11,12,13,14,15} };

    //Length of outer array which is actually Row Count;
    System.out.println("Rows   : " + simpleArray.length);

    //Length of inner array which is actually Column Count;
    //Notice that we take the first Row to get the Column length
    System.out.println("Columns: " + simpleArray[0].length);

    //Call the printIntegerList method with int[][] parameter
    printIntegerArray(simpleArray);
}

private static void printIntegerArray(int[][] intArray) {
    for(int i = 0; i < intArray.length; i++ ) {
        for(int j = 0; j < intArray[i].length; j++ ) {
            System.out.printf("%3d ", intArray[i][j]);
        }
        System.out.println();
    }
}

}

And the output to the console is as below;

Rows   : 3
Columns: 5
  1   2   3   4   5 
  6   7   8   9  10 
 11  12  13  14  15 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM