multidimensional array initialized with array returned by function

Viewed 41

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 have the second dimension be the array that is returned from a method. I do not understand why it does not work, as the error I am getting in Eclipse is

The left-hand side of an assignment must be a variable

From my understanding, I am creating a new int array and assigning the returned value to it. Populating the second dimension array immediately like this

new int[] {1, 2 ,3}

works like a charm, and I am looking to do something similar with the array that's returned to me from returnArray()

Any help is greatly appreciated.

p/

2 Answers
Related