The Java method iterates through an array once returning two results (maximum and minimum values)

Source: Internet
Author: User

    1. To return multiple parameters by using an inner class to store the results to be returned
    2. Using generics and qualifying types must implement the comparable interface for reuse
    3. Using T...A to implement an array that passes in a non-quantitative number
 Public classarrayalg{/*** Limit incoming type must implement comparable interface, passing in an indefinite number of arrays *@paramA *@param<T> *@returnreturns a static inner class that implements the return of multiple parameters*/     Public Static<textendsComparable> pair<t>Minmax (t...a) {if(a==NULL|| A.length==1)return NULL; T min=a[0]; T Max=a[0];  for(T t:a) {if(T.compareto (min) <0) min=T; if(T.compareto (max) >0) max=T; }        return NewPair<>(Min,max); }    /*generic Generic lookup minimum function public static <t extends comparable> t min (t[] array) {T min=array[0];        for (t T:array) {if (Min.compareto (t) >0) min=t;    } return min; }*/    Static classPair<t>{        PrivateT first; PrivateT Second;  PublicPair () {} PublicPair (T first,t second) { This. first=First ;  This. second=second; }         PublicT GetFirst () {returnFirst ; }         Public voidSetfirst (T first) { This. first=First ; }         PublicT Getsecond () {returnsecond; }         Public voidSetsecond (T second) { This. second=second; }    }}classtest{ Public Static voidMain (string[] argv) {Arrayalg.pair Pair=arrayalg.minmax ("ASD", "G", "C", "GHK"); System.out.println ("Min" +pair.getfirst () + "Max:" +Pair.getsecond ()); }}

The Java method iterates through an array once returning two results (maximum and minimum values)

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.