简体   繁体   English

将int [] []作为泛型参数传递

[英]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 . 我正在尝试传递一个二维数组, int[][] arr

I cannot use func1(arr) , but I can use func2(arr) 我不能使用func1(arr) ,但我可以使用func2(arr)

Can someone explain me how this works? 谁能解释一下这是如何工作的?

T[] represents an array of some generic object. T[]表示某个通用对象的数组。 Any array type (including int[] ) is an object. 任何数组类型(包括int[] )都是一个对象。 Therefore, int[][] is a valid T[] when T = int[] . 因此,当T = int[]时, int[][]是有效的T[] T = int[]

However, because int is not an object, int[][] is not a valid T[][] . 但是,因为int不是对象,所以int[][]不是有效的T[][]

If you you use Integer instead of int , you should be able to: 如果您使用Integer而不是int ,您应该能够:

  • call func1 with Integer[][] arr Integer[][] arr调用func1
  • call func2 with Integer[] arr or Integer[][] arr Integer[] arrInteger[][] arr调用func2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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