The method foo(T[]) in the type Class is not applicable for the arguments (Vector<Integer>)

andreauttini :

Can someone explain me what T[] does it mean? what can be accepted and why?

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

   }

}
cameron1024 :

T[] is an array of Ts. Just like how a String[] is an array of Strings.

The method signature: public static <T extends Comparable<T>> int[] foo(T[] s) means that:

  • The method takes an array of T
  • T extends Comparable
  • the method returns an int[]

In short, this means that: when getting objects from your array, you know you can call .compare() on them, and that it will return an int in the standard Comparable format.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=310689&siteId=1