将每列上找到的最大值添加到新数组(java)

时间:2017-11-09 15:24:09

标签: java arrays

在每列上搜索最大值后,如何将找到的值添加到新数组?

double cur[][] ={ 
        {  0,     0.25,   0.5},
        {  0.5,   0.75,   1},
        {  0.75,  1,      1},
        {  0,     0.25,   0.5},
        {  0.25,  0.5,    0.75},
        {  0,     0,      0.25},
        {  0.5,   0.75,   1}
};  

public void max1() {
    for (int i = 0; i < cur[0].length; i++) { // i should be your column
        double max = cur[0][i];// assign 1st value of the column as max
        for (int j = 0; j < cur.length; j++){ // j is your row
            if (cur[j][i] > max){ // check the column elements instead of row elements
                max = cur[j][i];// get the column values 
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

public void max1() {
    double[] max_values = new double[cur[0].length];
    for (int i = 0; i < cur[0].length; i++) { // i should be your column
        double max = cur[0][i];// assign 1st value of the column as max
        for (int j = 0; j < cur.length; j++){ // j is your row
           if (cur[j][i] > max){ 
              max = cur[j][i];// get the column values 
           }
        }
        max_values[i] = max;
    }

答案 1 :(得分:0)

首先创建一个新数组,然后在每个列中添加max元素。

int i = 0; float [] maximum = new float [3]; (因为只有3列)

第一个&#39; for循环&#39;从每列中获得最大值的地方加上这个,最大[i] = max&amp;我++;

希望它有所帮助。

答案 2 :(得分:0)

    double[] columnMaxima = IntStream.range(0, cur[0].length)
            .mapToDouble(
                    i -> Stream.of(cur)
                            .mapToDouble(row -> row[i])
                            .max()
                            .getAsDouble())
            .toArray();