学习内容:
用一道题目来练习ArrayList的遍历和访问操作。
例题:
解:
点击查看代码
import java.util.ArrayList;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(n != 0){
ArrayList<Integer> array = new ArrayList<Integer>();
for (int i = 0; i < n ; i++ ){
array.add(sc.nextInt());
}
int sum = 0;
for(int a : array){
sum += a;
}
int average = sum / array.size();
int result = 0;
for(int a : array){
if(a > average){
result += a - average;
}
}
System.out.println(result);
System.out.println();
n = sc.nextInt();
}
sc.close();
}
}