Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing int[][] as generic parameter

Tags:

public static <T> void func1(T[][] arr) {
    ...
}

public static <T> void func2(T[] arr) {
    ...
}

I'm trying to pass a 2-dimensional array, int[][] arr.

I cannot use func1(arr) , but I can use func2(arr)

Can someone explain me how this works?

like image 349
Sumit Das Avatar asked Nov 17 '18 21:11

Sumit Das


2 Answers

T[] represents an array of some generic object. Any array type (including int[]) is an object. Therefore, int[][] is a valid T[] when T = int[].

However, because int is not an object, int[][] is not a valid T[][].

like image 82
Joe C Avatar answered Sep 21 '22 12:09

Joe C


If you you use Integer instead of int, you should be able to:

  • call func1 with Integer[][] arr
  • call func2 with Integer[] arr or Integer[][] arr
like image 34
Taher A. Ghaleb Avatar answered Sep 21 '22 12:09

Taher A. Ghaleb