Java generic Method

Source: Internet
Author: User

Package pairtestmycode. copy; <br/> Import Java. util. *; <br/> public class pairtest2 <br/> {<br/> Public static void main (string [] ARGs) <br/> {<br/> gregoriancalendar [] Birthdays = <br/> {<br/> New gregoriancalendar (1906, calendar. december, 9), // G. hopper <br/> New gregoriancalendar (1815, calendar. december, 10), //. lovelace <br/> New gregoriancalendar (1903, calendar. december, 3), // J. von norann <br/> New gregoriancalendar (1910, calendar. june, 22), // K. zuse <br/>}; <br/> pair <gregoriancalendar> MM = arrayalg. MINMAX (birthdays); <br/> system. out. println ("min =" + mm. getfirst (). gettime (); <br/> system. out. println ("max =" + mm. getsecond (). gettime ()); <br/>}< br/> class arrayalg <br/>{< br/>/** <br/> gets the minimum and maximum of an array of objects of type T. <br/> @ Param a an array of objects of type T <br/> @ return a pair with the min and max value, or null if A is <br/> null or empty <br/> */<br/> Public static <t extends comparable> pair <t> MINMAX (T []) <br/>{< br/> if (a = NULL |. length = 0) return NULL; <br/> T min = A [0]; <br/> T max = A [0]; <br/> for (INT I = 1; I <. length; I ++) <br/>{< br/> If (min. compareto (A [I])> 0) min = A [I]; <br/> If (max. compareto (A [I]) <0) max = A [I]; <br/>}< br/> return new pair <t> (Min, max ); <br/>}< br/> class pair <t> <br/>{< br/> private t first; <br/> private t second; </P> <p> Public pair () <br/>{< br/> first = NULL; <br/> second = NULL; <br/>}< br/> Public pair (t first, t second) <br/>{< br/> This. first = first; <br/> This. second = Second; <br/>}< br/> Public t getfirst () <br/>{< br/> return first; <br/>}< br/> Public t getsecond () <br/>{< br/> return second; <br/>}< br/> Public void setfirst (T newvalue) <br/>{< br/> first = newvalue; <br/>}< br/> Public void setsecond (T newvalue) <br/>{< br/> second = newvalue; <br/>}</P> <p >}< br/>

 

30th rowsCodeYesPublic static <t extends comparable> pair <t> MINMAX (T [] A) <br/> {<br/> if (a = NULL |. length = 0) return NULL; <br/> T min = A [0]; <br/> T max = A [0]; <br/> for (INT I = 1; I <. length; I ++) <br/>{< br/> If (min. compareto (A [I])> 0) min = A [I]; <br/> If (max. compareto (A [I]) <0) max = A [I]; <br/>}< br/> return new pair <t> (Min, max ); <br/>}

 

Public static <t extends comparable> pair <t> MINMAX (T [])

This code is from page p665 of 7th of Java2 core technology version 1st. Although this book is a classic book, it is really hard to write in this place.

I haven't understood it for a long time. I went to Xinhua Bookstore and opened two books, Li Xinghua's "Java development practices" and Li Gang's "Crazy Java handouts ".

After the related part, I finally understood it. It seems that sometimes when a piece of complex technical code is just getting in touch, it is not necessarily stupid, but it is likely that

The author's reason.

Syntax of generic methods

[Access permission modifier] [static] [Final] <type parameter list> return value type method name ([format parameter list])

 

[] Is optional.

 

Public static <t extends comparable> pair <t> MINMAX (T [])

<T extends comparable> refers to the type parameter list. In the original words of the book Li Xinghua, t is an unknown type and is called by the generic method itself) to determine the type. [The generic type passed in or returned in a method is determined by the parameter type set when the method is called. Original sentence of Li Xinghua]

The extends and super keywords can be used to specify the upper limit and lower limit types respectively.

 

Pair <t> is the return value.

 

MINMAX parameter name

 

T [] form a parameter list

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.