2014-12-04 55 views
1

我试图将2d数组中每行的最大值更改为0.我遇到的问题是它将最高值更改为零,但它也将最高值之后的值更改为零。这里是我的代码:java 2d数组将每行的最高值更改为零

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 

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

回答

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] + " "); 
      } 
     } 
    } 
2

下面是代码为每行所做的事情。

  1. 获取第一个值为h
  2. 如果当前值大于h,请将其设置为0,然后将h设置为该值,即现在的0
  3. 所有后续值为正,所以他们大于h0),使他们获得设置为0也。

你应该做的,而不是:

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

我觉得你过于复杂,你是如何去掉最高值的指数的最大值设置为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。

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