簡體   English   中英

具有不同行長的二維數組。 我需要刪除最小值,最大值並計算Java中每一行的平均值

[英]Two dimensional array with different row length. I need to drop min, max and calculate the average for each line in Java

public class Average {
static int[][] myDouble = new int[10][12];
static int x = 0, y = 0;
static int strDouble;

public Average() {
    try {
        BufferedReader in = new BufferedReader(new FileReader("StudentIdAverage.txt"));
        String line;
        while ((line = in.readLine()) != null) {
            String[] values = line.split("\\s+");

            for (String str : values) {
                strDouble = Integer.parseInt(str);
                myDouble[x][y] = strDouble;
                y = y + 1;
            }
            x = x + 1;
            y = 0;
        }
        in.close();
    } catch (IOException ioException) {
    }
}

public static void main(String[] args) {

    Average arr = new Average();
    //int[][] residuescores = arr.myDouble;
    for (int i = 0; i < myDouble.length; ++i) {
         int sum = 0;
         int average = 0;
        for (int j = 0; j < myDouble[i].length; ++j) {
            Arrays.sort(myDouble[i]);
            sum+=myDouble[i][j];
            System.out.print(Average.myDouble[i][j] + " ");
        }
        average = (sum/myDouble[i].length);

        System.out.println(" ");
        System.out.println(average);
    }

}
}

輸入文件:-

45 72 90 50 67 63 81 71 55 56 80 74 / n 55 54 79 72 75 68 / n 51 88 79 72 / n 98 52 52 53 50 92 67 99 92 50 61 91 / n 94 48 53 92 97 / n 97 69 77 74 68 54 87 74 54 83 58 69 / n 75 49 87 61 66 53 79 48 96 60 / n 58 71 51 73 53 75 93 81 45 69 78 65 / n 50 88 78 81 99 61 97 70 87 80 69 / n 91 89 97 80 93 82 92 49 52 69 96 61

如果您使用的是Java 8,則可以利用流和SummaryStatistics計算平均值/最小值/最大值,如下所示

static Integer[][] myDouble = new Integer[10][12];

for (int i = 0; i < myDouble.length; ++i) {
     IntSummaryStatistics statistics = Arrays.asList(myDouble[i]).stream().filter(intValue -> intValue!=null).collect(Collectors.summarizingInt(Integer::intValue));
     System.out.println("Average: "+statistics.getAverage()+", min: "+statistics.getMin()+", max: "+statistics.getMax());
}

如果您不能使用Java 8,那么請注意,代碼的問題是您正在數組中使用原始int,該類型會將值初始化為0,因此當行的值小於12時,您將獲得0。一種解決方法這是將您的數組更改為Integer類,但是請記住跳過具有null的條目,因為現在的條目不是0,而不是0。

您將使用int數組的代碼更改為Integer,跳過空值並使用count而不是array.length:

  for (int i = 0; i < myDouble.length; ++i) {
    int count = 0; // count the values used to calculate sum
     int sum = 0;
     int average = 0;
    for (int j = 0; j < myDouble[i].length; ++j) {
        if(myDouble[i][j] == null) //skip the null values
            continue;
        //Arrays.sort(myDouble[i]);
        sum+=myDouble[i][j];
        count++;
        System.out.print(App.myDouble[i][j] + " ");
    }
    average = (sum/count); //use count instead of lenght

    System.out.println(" ");
    System.out.println(average);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM