Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol - class T

Tags:

java

generics

I've this function;

  public static T[] addToArray(T item, T... items){
    T[] array;
    int array_size = 1;

    if(items !=null){ array_size = items.length+1; }

    array = java.util.Arrays.copyOf(items, array_size);
    array[array_size-1] = item;

    return array;
  }

And I get this error cannot find symbol symbol: class T. The idea is to make this method generic. I never worked with generics so I'm guessing I miss some reference?

like image 676
Nir Avatar asked Sep 13 '13 13:09

Nir


1 Answers

Method signature for generic method is as follows

 public static <T> T[] addToArray(T item, T... items){
      T[] array;
      int array_size = 1;

      if(items !=null){ array_size = items.length+1; }

      array = java.util.Arrays.copyOf(items, array_size);
      array[array_size-1] = item;

      return array;
    }
like image 57
Siva Avatar answered Sep 22 '22 21:09

Siva