20
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?

Muntasir
  • 798
  • 1
  • 14
  • 24
Sumit Das
  • 1,007
  • 9
  • 17

2 Answers2

25

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

Joe C
  • 15,324
  • 8
  • 38
  • 50
2

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
Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44