JAVA core technology Volume 1, generic example, java Core Technology example

Source: Internet
Author: User

JAVA core technology Volume 1, generic example, java Core Technology example

For this Code, many netizens are full of questions. First of all, there are a lot of questions. If you don't write the Pair class, you can compile it.

The Pair class is on the first two pages of other code.


Here, the characteristics of generics are written. Several constructors and parameter changes explain the generics.

Package pair1;


Public class Pair <T> {
Private T first;
Private T second;
Public Pair (){
First = null;
Second = null;
}

Public Pair (T first, T second ){
This. first = first;
This. second = second;

}

Public T getFirst (){
Return first;
}
Public T getSecond (){
Return second;
}

Public void setFirst (T newValue ){
First = newValue;

}

Public void setSecond (T newValue ){
Second = newValue;

}



}




Package pair1;



Public class ArrayAlg {
Public static Pair <String> minmax (String [] ){
If (a = null | a. length = 0)
Return null;
String min = a [0];
String max = a [0];
For (int I = 1; I <a. length; I ++ ){
If (min. compareTo (a [I])> 0)
Min = a [I];
If (max. compareTo (a [I]) <0)
Max = a [I];
}

Return new Pair <> (min, max );
}


}



Package pair1;


Public class PairTest1 {


Public static void main (String [] args ){
// TODO Auto-generated method stub
String [] words = {"Mary", "had", "a", "little", "lamb "};
Pair <String> mm = ArrayAlg. minmax (words );
System. out. println ("min =" + mm. getFirst ());
System. out. println ("min =" + mm. getSecond ());
}


}


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.