0:00

What are the contents of arr after the following code has been executed?

int[][] arr = {{3,2,1},{1,2,3}};
int value = 0;
for (int row = 1; row < arr.length; row++) {
   for (int col = 1; col < arr[0].length; col++) {
      if (arr[row][col] % 2 == 1)
      {
          arr[row][col] = arr[row][col] + 1;
      }
      if (arr[row][col] % 2 == 0)
      {
          arr[row][col] = arr[row][col] * 2;
      }
   }
}
注意循环跳过了第一行和第一列,且两个if均会被执行。
3

A two-dimensional array, imagePixels, holds the brightness values for the pixels in an image. The brightness can range from 0 to 255. What does the following method compute?

public int findMax(int[][] imagePixels) {
   int r, c;
   int i, iMax = 0;

   for (r = 0; r < imagePixels.length; r++) {
      for (c = 0; c < imagePixels[0].length; c++) {
         i = imagePixels[r][c];
         if (i > iMax)
            iMax = i;
      }
   }
   return iMax;
}
典型的数组最大值算法。
1


陈 欣

AADPS创始人

发表评论