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 ());
}
}