简体   繁体   English

计算机科学:Java MC

[英]Computer Science: Java MC

private int[] myStuff;
/** Precondition: myStuff contains int values in no particular order.
  /*/ 
public int mystery(int num)
{
    for (int k = myStuff.length-1; k>=0; k--)
    {
        if (myStuff[k] < num)
        {
           return k;
        }
    }
return -1;
}

Which of the following best describes the contents of myStuff after the following statement has been executed? 在执行以下语句后,以下哪项最能描述myStuff的内容?

int m = mystery(n); 

Answer: All values in positions m+1 through myStuff.length-1 are greater than or equal to n. 答案:位置m + 1到myStuff.length-1的所有值都大于或等于n。

Can anyone explain why this answer is correct? 谁能解释为什么这个答案是正确的? I'm not sure what they mean by contents but, I concluded that myStuff is unchanged because the code doesn't alter the value of myStuff. 我不确定它们的含义是什么,但我得出结论,myStuff没有改变,因为代码不会改变myStuff的值。

Precondition says, that myStuff contains integer values in no particular order. 前提条件说, myStuff包含没有特定顺序的整数值。 The for loop is initialized with k equal to the lenght of myStuff minus one (since array indices start at zero), iterates while k is greater than or equal to zero and subtract one from k in each iteration. for循环初始化为k等于myStuff的长度减1(因为数组索引从零开始),迭代k大于或等于零,并在每次迭代中从k减去1。

If you look at the if statement, it will return the current value of k , when the value at the corresponding index of myStuff[k] is less than n ; 如果查看if语句,当myStuff [k]的相应索引处的值小于n时,它将返回k的当前值; hence all values in array indices greater than the returned value plus one, must be greater than or equal to n . 因此,数组索引中的所有值都大于返回值加1,必须大于或等于n And if no numbers integers in myStuff are greater than n , minus one is returned. 如果myStuff中的数字整数大于n ,则返回减1。

I hope this helps. 我希望这有帮助。

Btw - you're right that the content of mySyuff isn't changed. 顺便说一句 - 你是对的, mySyuff的内容没有改变。

You are correct that myStuff is unchanged. 你的myStuff没有改变是正确的。 However, they are asking about the relationship between the data in the array, the return value of the function, and the function's arguments. 但是,他们询问数组中的数据,函数的返回值和函数的参数之间的关系。

The code does the following: 代码执行以下操作:

  • Iterates the array in reverse order. 以相反的顺序迭代数组。 (for loop) (for loop)
  • At each index during the iteration, it checks the value of that index against n , returning the current index if n is larger. 在迭代期间的每个索引处,它检查该索引的值对n ,如果n更大则返回当前索引。 (if statement) (如果声明)

So what does the method return? 那么该方法返回什么? It returns the first index from the end where mystuff[m] < n . 它返回mystuff[m] < n末尾的第一个索引。 ( mystuff[i] >= num ) mystuff[i] >= num

The question about the content is valid. 关于内容的问题是有效的。 It has nothing to with whether the content itself was changed. 它与内容本身是否发生了变化无关。

This method returns either a non-negative number when there is such k (from myStuff.length-1 to 0 ) that myStuff[k] is less than n , or -1 when all elements of myStuff are more than or equal to n . 当存在myStuff[k]小于n k (从myStuff.length-10 )时,此方法返回非负数,或者当myStuff 所有元素大于或等于n时返回-1

Let's consider both cases: 让我们考虑两种情况:

  • m is non-negative number - it means that position m was the last from the right that satisfied the condition myStaff[m] < n , your answer is correct m是非负数 - 这意味着位置m是满足条件myStaff[m] < n 的右边的最后一个,你的答案是正确的

  • m is -1 - it means that all elements are more than equal than m+1 (which in this case is 0 ) - hence the answer is correct m是-1 - 这意味着所有元素都大于等于m+1 (在这种情况下为0 ) - 因此答案是正确的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM