1
public static void main(String args[])
{

    int[] intarray = {1, 3, 6, 8, 2, 6};        
    String[] names = {"String1", "String2", "String3", "String4", "String5", "String6"};

    printMe(intarray);

}

public static <T> void printMe(T[] i){
    for(T x: i)
    {
        System.out.println(x);
    }

}

Why does compiling this code result in this error?

The method printMe(T[]) is not applicable for the arguments (int[])

If I do printMe(names) then it works.

finnw
  • 47,861
  • 24
  • 143
  • 221
S L
  • 14,262
  • 17
  • 77
  • 116
  • possible duplicate of [How to convert int\[\] to Integer\[\] in Java?](http://stackoverflow.com/questions/880581/how-to-convert-int-to-integer-in-java) – finnw May 24 '11 at 14:51
  • Also related to: http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should – finnw May 24 '11 at 14:57

3 Answers3

4

Because its array of int not of Integer, its expecting a class over there

jmj
  • 237,923
  • 42
  • 401
  • 438
  • thanks a lot. but what's the difference between int and Integer? Is Integer defined as `Integer Object` by the system (since I've not defined it)? – S L Mar 15 '11 at 06:24
  • `int` is primitive data type, where `Integer` is class – jmj Mar 15 '11 at 06:25
  • 1
    `Integer` is [`java.lang.Integer`](http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html) – asgs Mar 15 '11 at 06:26
  • I mean is it defined by the system? (i don't remember defining an object name Integer) – S L Mar 15 '11 at 06:26
  • yes it is included with standard lib. check http://download.oracle.com/javase/6/docs/api/java/lang/Integer.html – jmj Mar 15 '11 at 06:27
1

Simple. Generics are meant for Object-based datatypes and not for primitives.

asgs
  • 3,928
  • 6
  • 39
  • 54
1

Simple. Generics are meant for Object-based datatypes and not for primitives. In the case of String array it is type casting to object type, in case int array automatically it is not casted to Object type, So either explicitely another method to be included or make it Integer.