2014-12-04 5 views
1

2 차원 배열의 각 행의 가장 높은 값을 0으로 변경하려고합니다. 문제는 가장 높은 값을 0으로 변경한다는 것입니다. 또한 가장 높은 값 다음의 값을 0으로 변경합니다. 원하는 출력이 있지만java 2d 배열은 각 행의 가장 높은 값을 0으로 변경합니다.

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 

나는 단지 최대 값을 변경하지 만들 수있는 방법

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

전류 출력은 이것이다 : 여기에 내 코드입니다 다른 사람?

답변

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. 모든 후속 값은 양수이므로 h (0)보다 크므로 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
에 그냥 인덱스를 설정 : 나는이 같은 일을 할 것입니다 코드에서

는 :

대신 당신이해야 array[a][b] = 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]; 
      } 
     } 
    } 

먼저 h = array[a][b]을 설정합니다.

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; 
      } 
     } 
    } 
: 그럼 당신은 변수에 설정하여 b 인덱스를 추적해야 yIndex = b; 마지막으로, 그래서 당신이 뭔가를해야 0

에 위치 [a][yIndex]의 배열을 설정

관련 문제