For this code, a lot of netizens are full of questions, first there are many, not careful, did not write the pair class, to compile.
Certainly compiled, the pair class is on the first two pages of the other code.
And here, the features of generics are written. By several constructors, and the change of parameters, the generic type is explained.
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[] a) {
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];
}
< /span>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 ());
}
}
Java Core Technology Volume one, generic example