类类型中的方法 foo(T[]) 不适用于参数 (Vector<Integer>)



有人能解释一下这是什么T[]意思吗? 什么可以接受,为什么?

public class Class {
public static <T extends Comparable<T>> int[] foo(T[] s) {
// ...
}
public static void main(String[] args) {
int[] intArray1 = new int[10];
int[] ris = foo(intArray1);  // nope
Vector<Integer> intArray2 = new Vector<Integer>(10);
int[] ris = foo(intArray2);  // nope
Integer[] intArray3 = new Integer[10];
int[] ris = foo(intArray3);  // ok
}
}

T[]是一个T数组。就像String[]String数组一样。

方法签名:public static <T extends Comparable<T>> int[] foo(T[] s)意味着:

  • 该方法采用一系列T
  • T 扩展 可比
  • 该方法返回一个int[]

简而言之,这意味着:从数组中获取对象时,您知道可以对它们调用.compare(),并且它将以标准Comparable格式返回int

相关内容

最新更新