- To return multiple parameters by using an inner class to store the results to be returned
- Using generics and qualifying types must implement the comparable interface for reuse
- 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)