2014-12-04 26 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; 
      } 
     } 
    } 
相關問題