简体   繁体   中英

Passing int[][] as generic parameter

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM