import java.util.Arrays;
public class MaxAndMinPair extends Object{
private int min;
private int max;
public MaxAndMinPair(){
}
public MaxAndMinPair(int myArrayInt[]){
maxAndMin(myArrayInt);
}
public void setMax(int max){
this.max = max;
}
public void setMin(int min){
this.min = min;
}
public int getMax(){
return max;
}
public int getMin(){
return min;
}
public static void maxAndMin(int myArrayInt[], MaxAndMinPair aPair){
Arrays.sort(myArrayInt);
aPair.setMin(myArrayInt[0]);
aPair.setMax(myArrayInt[myArrayInt.length-1]);
}
private void maxAndMin(int myArrayInt[]){
Arrays.sort(myArrayInt);
min = myArrayInt[0];
max = myArrayInt[myArrayInt.length-1];
}
}
public class test {
public static void main(String args[]) {
/*
* Method 1
*/
int[] myArrayInt = {1,2,5,4,9,8,7,6,3,5,4,5};
int[] myArrayInt1 = {7,52,26,45,90,81,72,63,36,51,46,59};
MaxAndMinPair aPair = new MaxAndMinPair();
MaxAndMinPair.maxAndMin(myArrayInt, aPair);
System.out.println("Max:" + aPair.getMax());
System.out.println("Min:" + aPair.getMin());
/*
* Method 2
*/
MaxAndMinPair anotherPair = new MaxAndMinPair(myArrayInt1);
System.out.println("Max:" + anotherPair.getMax());
System.out.println("Min:" + anotherPair.getMin());
}
}
温馨提示:内容为网友见解,仅供参考