I am having a problem in Generic Methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khalid galal
    New Member
    • Apr 2008
    • 16

    I am having a problem in Generic Methods

    Hi, I am having a problem in Generic Methods, look at this class
    class ArrayAlg{
    public static <T> T getMiddle(T[] a)
    {
    return a[a.length / 2];
    }
    }
    The method "getMiddle" works well when i send to it a String[] as a parameter but when i send to it an int[] as a parameter it throws this exception

    <T>getMiddle( T[]) in generic1.ArrayA lg cannot be applied to (int[])
    incompatible types
    found getMiddle
    required int[]
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    int is a java primitive data type, String is a class.

    i guess you should use Integer[] instead of int[]....

    Im not sure.....

    Comment

    • khalid galal
      New Member
      • Apr 2008
      • 16

      #3
      Thanks very much, your suggestion worked well.

      Comment

      • khalid galal
        New Member
        • Apr 2008
        • 16

        #4
        Thanks very much, your suggestion worked well.

        Comment

        Working...