JAVA编程1、 求一维数组的最大值和最小值

如题所述

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

}
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-05-19
此处略去N个数组初始化操作字符
int max=arr[0];
int min=arr[0];
for(int i=0;i<arr.length;i++){
if(arr[i]>max]) max=arr[i];
if(arr[i]<min) min=arr[i]
}
此处略去N个后续操作字符
相似回答