java 2d数组将每行的最高值更改为零

时间:2014-12-04 22:47:17

标签: java arrays

我正在尝试将2d数组中每行的最高值更改为0.我遇到的问题是它将最高值更改为零,但它也会将最高值后的值更改为零。这是我的代码:

public class Test2 {
  public static double[][] array1 = //array to be used
   {{7.51, 9.57, 6.28, 5.29, 8.7},
    {8.07, 6.54, 5.44, 8.78, 8.66},
    {9.34, 9.73, 7.19, 6.87, 6.48}};
  public static void main(String[] args) {
    double h = array1[0][0];// highest value 

    for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
      h = array1[a][0];
      for(int b = 0; b < array1[a].length; b++) {
        if ( array1[a][b] > h){
          array1[a][b] = 0;
          h = array1[a][b];
        }
      }
    }
    System.out.println(); //print array with highest values in each row now changed to zero
    for (int x = 0; x < array1.length; x++){
     System.out.println();
     for (int y = 0; y < array1[x].length; y++){
      System.out.print(array1[x][y] + " "); 
     }
    }    
   }
  }

目前的输出是:

7.51 0.0 0.0 0.0 0.0 
8.07 6.54 5.44 0.0 0.0 
9.34 0.0 0.0 0.0 0.0 

虽然所需的输出是这样的:

7.51 0.0 6.28 5.29 8.7 
8.07 6.54 5.44 0.0 8.66 
9.34 0.0 7.19 6.87 6.48 

我怎样才能让它只改变最大值而不改变其他值?

4 个答案:

答案 0 :(得分:2)

这是代码为每一行做的事情。

  1. 获取第一个值h
  2. 如果当前值大于h,请将其设置为0,然后将h设置为该值,现在为0
  3. 所有后续值都是正数,因此它们大于h0),因此它们也会设置为0
  4. 你应该做什么:

    1. 获取行的最大值,记下最大值的索引。
    2. 在行的末尾,使用步骤1中存储的索引将最大值设置为0

答案 1 :(得分:0)

我认为您过度复杂化了如何消除最高价值。我会做这样的事情:

int highestIndex = 0;
for (int i = 0; i < array1.length; i++)
{
    for (int j = 0; j < array1[i].length; j++)
        if (array1[i][j] > array1[i][highestIndex])
            highestIndex = j;
    array1[i][highestIndex] = 0;
}

这样做是循环遍历每一行,找到最高值的索引,然后将该索引设置为0。

答案 2 :(得分:0)

public static double[][] array1 = // array to be used
    { { 7.51, 9.57, 6.28, 5.29, 8.7 }, { 8.07, 6.54, 5.44, 8.78, 8.66 }, { 9.34, 9.73, 7.19, 6.87, 6.48 } };

    public static void main(String[] args) {

        //loop through each row
        for(int x = 0; x<array1.length; x++){
            //make sure not empty (won't be an issue with the supplied array, but could be)
            if(array1[x].length > 0){
                //take the number as a starting place for your highest index
                //you don't want to pick 0 as the highest number in case all of the numbers are negative
                int highestIndex = 0;
                double highestNumber = array1[x][0];

                //look at the rest of the numbers and see if there are any that are higher
                for(int y = 1; y<array1[x].length; y++){
                    if(array1[x][y] > highestNumber){
                        highestIndex = y;
                        highestNumber = array1[x][y];
                    }
                }

                //set whatever is the highest to 0
                array1[x][highestIndex] = 0;
            }
        }

        System.out.println(); // print array with highest values in each row now
                                // changed to zero
        for (int x = 0; x < array1.length; x++) {
            System.out.println();
            for (int y = 0; y < array1[x].length; y++) {
                System.out.print(array1[x][y] + " ");
            }
        }
    }

答案 3 :(得分:0)

在您的代码中:

for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
        h = array1[a][0];
        for(int b = 0; b < array1[a].length; b++) {
            if ( array1[a][b] > h){
                array1[a][b] = 0;
                h = array1[a][b];
            }
        }
    }

而不是设置array[a][b] = 0,您应首先设置h = array[a][b]。然后,您应该通过在变量b中设置它来跟踪yIndex = b;索引,最后将数组设置为[a][yIndex]0

所以你应该有这样的东西:

    for ( int a = 0; a < array1.length; a++){ //loop through array and change highest values in each row to zero
        h = array1[a][0];
        for(int b = 0; b < array1[a].length; b++) {
            if ( array1[a][b] > h){
                h = array1[a][b];
                int yindex = b;
                array1[a][yindex] = 0;
            }
        }
    }