数组的程序跟踪?

时间:2019-11-05 04:33:57

标签: arrays

我正在跟踪该程序,但似乎无法获取。有人可以解释吗?

Question    5.
Consider    the following   code    fragment:

  int [ ] x = {0, 1, 2, 3};
  int temp;
  int i = 0;
  int j = x.length – 1;
    while( i < j ){
      temp = x[i];
      x[i] = x[j];
      x[j] = 2 * temp;
      i++;
      j--;
}

After   this    code    is  executed,   array   “x” contains    the values:
a) {3,  2,  2,  0}
b) {0,  1,  2,  3}
c) {3,  2,  1,  0}
d) {0,  2,  4,  6}
e) {6,  4,  2,  0}

我知道答案是字母a,因为我对其进行了编译,但是我在追查中遇到了困难,因为我     似乎得到了

Question    7.
  Consider  the following   code    fragment:
  int[ ] array1 = {2, 4, 1, 3};
  int[ ] array2 = {0, 0, 0, 0};
  int a2 = 0;   
  for(int a1 = 1; a1<array1.length; ++a1)
   {
    if( array1[a1] >= 2)
   {
   array2[a2] = array1[a1];
   ++a2;
   }
  }

执行此代码后,数组“ array2”包含的内容
    值?

a) {4,  3,  0,  0}
b) {4,  1,  3,  0}
c) {2,  4,  3,  0}
d) {2,  4,  1.  3}

0 个答案:

没有答案