# 直接找出数组中的最大值或者最小值的方法

### 不要进行排序, 直接找出一个数组中的最大值和最小值, (百度的时候还会出现, 次大值的情况)

#### 直接进行数据的比较不就好了, 这么简单…

public class NumMAXTest {

public static void main(String[] args) {

int[] num = {1,2,3,4,6,5,7,8,-10,-9,1,2,3,4,6,5,7,8,-10,-9,-11,19};
//int[] num = {};
FindMAXAndMIN findMAXAndMIN = new FindMAXAndMIN(num);

System.out.println("长度为:" + findMAXAndMIN.getLength());
System.out.println("此数组最大值为: " + findMAXAndMIN.getMaxNum());
System.out.println("此数组最小值为: " + findMAXAndMIN.getMinNum());

}
}

class FindMAXAndMIN {
private int[] num;
private int max_num;
private int min_num;

public FindMAXAndMIN(int[] num) {
this.num = num;
}

public int getMaxNum() {
if (num.length == 0) {
throw new ArrayStoreException("数组长度为0");
}
max_num = num[0];
for (int i=0; i<num.length; i++) {
if (num[i] > max_num){
max_num = num[i];
}
}
return max_num;
}

public int getMinNum() {
if (num.length == 0) {
throw new ArrayStoreException("数组长度为0");
}
min_num = num[0];
for (int i=0; i<num.length; i++) {
if (num[i] < min_num){
min_num = num[i];
}
}
return min_num;
}

public int getLength() {
return num.length;
}
}



05-16
02-17 7655

11-16 385
09-19 3283
08-05 4922
09-14 8995
03-30 180
01-23 2118
02-21 48
05-14 3万+