A multidimensional array initialized by the array returned by the function

I have a problem with the following example:

public static void main(String[] args) {

    // this works
    int[] test1DArray = returnArray();

    int[][] test2DArray = new int[][] {

        // this does not work
        new int[]= returnArray(),

        // yet this does
        new int[] {1, 2 ,3}
}

private static int[] returnArray() {

    int[] a = {1, 2, 3};
    return a;
}

I am looking for a way to create a two-dimensional array and the second dimension is the array returned by the method. I do not understand why this does not work, since the error I get in Eclipse is

The left side of the job must be variable

From my point of view, I create a new int array and assign it a return value. Filling a second array of measurements right away like this

new int[] {1, 2 ,3}

works like a charm and I'm looking for something similar with an array that was returning to me from returnArray()

Any help is greatly appreciated.

R/

+4
source share
2 answers

Just use:

int[][] test2DArray = new int[][] {
    returnArray(),
    new int[] {1,2 ,3}
};
+4
source

@Eran , , , .

, .

: new int[]{1, 2, 3} 1, 2 3 test2Darray, int[] n = new int[]{1, 2, 3} test2Darray. , .

returnValue() - , ( ). , new int[]{1, 2, 3}.

, new int[]{1, 2, 3} returnValue() .

+2

Source: https://habr.com/ru/post/1678556/


All Articles