Java Collection Framework

Discussion in 'Java' started by a, Dec 16, 2005.

  1. a

    a Guest

    For Collection interface, the declaration of toArray() is:
    <T> T[] toArray(T[] a);
    Why do we have <T> at the start of the method declaration?
    Thanks a lot
     
    a, Dec 16, 2005
    #1
    1. Advertising

  2. a

    Roedy Green Guest

    On Fri, 16 Dec 2005 04:44:37 GMT, "a" <> wrote, quoted or
    indirectly quoted someone who said :

    >For Collection interface, the declaration of toArray() is:
    ><T> T[] toArray(T[] a);
    >Why do we have <T> at the start of the method declaration?
    >Thanks a lot


    see http://mindprod.com/jgloss/generics.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 16, 2005
    #2
    1. Advertising

  3. a wrote:
    > For Collection interface, the declaration of toArray() is:
    > <T> T[] toArray(T[] a);
    > Why do we have <T> at the start of the method declaration?


    Because the "genius" that came up with the idea for Java generic decided
    that this should be the syntax when you have a parametric (generic)
    method and need to provide a type parameter.

    And you need to have th <T>, otherwise the compiler would not know that
    the two T[] in the method signature refer to a type parameter, instead
    of just a 'class T".

    /Thomas
    --
    The comp.lang.java.gui FAQ:
    ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
    http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
     
    Thomas Weidenfeller, Dec 16, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Moskowitz
    Replies:
    3
    Views:
    550
    David Moskowitz
    Feb 23, 2006
  2. David Moskowitz
    Replies:
    6
    Views:
    588
    Nick Kew
    Feb 23, 2006
  3. Pradeep
    Replies:
    2
    Views:
    687
    Patricia Shanahan
    Jan 24, 2007
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    1,002
    Øyvind Isaksen
    May 18, 2007
  5. Rishipal Singh
    Replies:
    3
    Views:
    131
    Jeff Higgins
    Apr 21, 2014
Loading...

Share This Page