2

whenever i am passing two 1-d array as argument to a 2-d array it is working fine, but when i try to pass three 1-d arrays to a 3-d array argument method then it is giving error but same works fine when i pass three 1-d array to 2-d array.

What is the reason behined this behaviour ?

Two 1-d array to a 2-d method:-

int[] c=new int[] {4,50};
        m1(new int[]{10,20},c);


    public static void m1(int[]... a)]

Three 1-d array to a 3-d method :- , error - The method m1(int[][]...) in the type Asd is not applicable for the arguments (int[], int[], int[])

    int[] b=new int[] {3,50};
int[] c=new int[] {4,50};
        m1(new int[]{10,20},c,new int[] {4,50});


    public static void m1(int[][]... a)

Three 1-d array to 2-d array:-

int[] c=new int[] {4,50};
        m1(new int[]{10,20},c,new int[] {4,50});
    }

    public static void m1(int[]... a)
5
  • 1
    Any particular reason you don't think including the error would be helpful? Commented Jul 29, 2017 at 19:34
  • 1
    Well int[][]... a expects 2d arrays, but you are passing 1d arrays. How do think this should work? Where 1d array should be placed in a which is 3d array? Commented Jul 29, 2017 at 19:38
  • You're always passing a 1-d array. Commented Jul 29, 2017 at 19:41
  • @Pshemo but then why did same works for 2-d array, passing two 1-d arrays for a 2-d array (example 1 in the question) Commented Jul 29, 2017 at 19:42
  • Multidimensional arrays are simply arrays of arrays which are one dimension level smaller, so 2d array can hold only 1d array, 3d array can hold only 2d array (which internally can hold 1d arrays). You can't place 1d array directly into 3d array because that would brake things. 3d array should let us use 3 indexes [x][y][z] but if array is { {1, 2}, {3} } then what should be result for a[0][0][0]? If that would be possible a[0] would return {1}, then a[0][0] would return 1 but then a[0][0][0] would be like calling 1[0] which doesn't make sense since 1 is not an array. Commented Jul 29, 2017 at 19:54

1 Answer 1

3

The ellipsis (...) is just an array.

int[]...a <=> int[][] a

int[][]...a <=> int[][][] a

in the second exemple the m1 method wait for a 3d array OR a list of 2d array, but you call it with a list of 1d array

the good call is :

m1(new int[][], new int[][] , ...)

and in the m1 method the a arg is of type int[][][]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.