2017-11-09 2 views
0

各列の最大値を検索したら、新しい配列にどのように値を追加するのですか?新しい列に見つかった最大値を新しい配列に追加する(java)

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 
      } 
     } 
    } 
} 
+0

あなたの質問はどのようにネットビーンズに関連していますか? –

+0

メソッドの型を変更する必要がありますので、voidではなくdouble [] []を返します。 – dave

+0

私はnetbeansを使用してJavaアプリケーションをコードします。 – DonQuixote

答えて

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; 
    } 
0

最初の新しい配列を作成し、あなたが得るとして、その中に各列からの最大要素を追加します。

int i = 0; float [] maximum =新しいfloat [3]; (3列しかないので)

各列から最大値を得る最初の 'forループ'の後に、これを加えると、最大[i] = max & i ++;

希望します。

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