- 求数组的总和
static int [] intArr = new int[]{30,96,23,69,85,62,12,99,11};
public static void main(String[] args) {
IntStream intStream =Arrays.stream(intArr);
int sum = intStream.sum();
System.out.println(“总和:”+sum);
}
输出:
总和:487
- 求数组最大值、最小值、总和、平均值
static int [] intArr = new int[]{30,96,23,69,85,62,12,99,11};
public static void main(String[] args) {
IntStream intStream =Arrays.stream(intArr);
int sum = intStream.sum();
intStream =Arrays.stream(intArr);
int max = intStream.max().getAsInt();
intStream =Arrays.stream(intArr);
int min = intStream.min().getAsInt();
intStream =Arrays.stream(intArr);
double avg = intStream.average().getAsDouble();
System.out.println(“最大值:”+max+“\n最小值:”+min+“\n总和:”+sum+“\n平均值:”+avg);
}
输出:
最大值:99
最小值:11
总和:487
平均值:54.111111111111114