我有一个快速的java程序来执行一些数字操作。现在它使用Collections库,但是赋值不使用java.util.Collections类中的任何方法。我可以用什么来切换它?现在我有Collections对ArrayList进行排序,并获得最小值和最大值。
谢谢。
如果您需要我的代码:
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;
class Main {
public static void main(String[] args) {
ArrayList<Integer> grades = new ArrayList<Integer>();
Scanner input = new Scanner(System.in);
System.out.println("Input:");
int i = 0;
int strs = 0;
strs = input.nextInt();
while (strs != -1) {
if(strs > 100 || strs < 0 && strs != -1) {
strs = input.nextInt();
}
else {
grades.add(strs);
strs = input.nextInt();
i++;
}
}
System.out.println("\nOutput:\n");
System.out.println(grades);
System.out.println("Sorted:");
Collections.sort(grades);
System.out.println(grades);
int sum = 0;
for (int g : grades) sum += g;
double average = sum / grades.size();
average = 1 * sum / grades.size();
System.out.println("Average: " + average);
int mid = grades.size()/2;
double median = grades.get(mid);
if (grades.size()%2 == 0) {
median = (median + grades.get(mid-1))/2;
}
System.out.println("Median: " + median);
int max = Collections.max(grades);
int min = Collections.min(grades);
int range = max - min;
System.out.println("Range: " + range);
}
}