英文:
Fixed Point (Value equal to index) in a given array using Linear Search - Logic question
问题
以下是您要翻译的部分:
"Can someone provide an explanation why using this method below returns only the first matching value within the array? For example if My array element is 0, 1, 2, 3 and 4. It will return 0, instead of 4. Every value within the array matches to its index 4, should using the for loop return 4 after the last iteration is completed?
static int linearSearch(int arr[], int n)
{
int i;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
return i;
}
/* If no fixed point present
then return -1 */
return -1;
}
//main function"
英文:
Can someone provide an explanation why using this method below returns only the first matching value within the array? For example if My array element is 0, 1, 2, 3 and 4. It will return 0, instead of 4. Every value within the array matches to its index 4, should using the for loop return 4 after the last iteration is completed?
static int linearSearch(int arr[], int n)
{
int i;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
return i;
}
/* If no fixed point present
then return -1 */
return -1;
}
//main function
答案1
得分: 3
这是因为在你的 if
语句中,你立即调用了 return
,而你的方法应该返回一个单独的 int
。如果你想收集所有的固定点,你可以修改你的方法如下:
static List<Integer> linearSearch(int arr[], int n)
{
List<Integer> fixedPoints = new ArrayList<>();
for(int i = 0; i < n; i++)
{
if(arr[i] == i)
fixedPoints.add(i);
}
return fixedPoints;
}
英文:
It's because in your if
you immediately call return
, and your method is supposed to return a single int
. If you want to collect all fixed points, you can modify your method like this:
static List<Integer> linearSearch(int arr[], int n)
{
List<Integer> fixedPoints = new ArrayList<>();
for(int i = 0; i < n; i++)
{
if(arr[i] == i)
fixedPoints.add(i);
}
return fixedPoints;
}
答案2
得分: 2
这将返回4,因为它具有每个i匹配到数组中的值的更新索引。
static int linearSearch(int arr[], int n)
{
int i, index = -1;
for (i = 0; i < n; i++)
{
if (arr[i] == i)
index = i;
}
/* 如果没有固定点存在
则返回-1 */
return index;
}
// 主函数
英文:
This will return 4 as it has an updated index for each i matching to its value in an array.
static int linearSearch(int arr[], int n)
{
int i,index = -1;
for(i = 0; i < n; i++)
{
if(arr[i] == i)
index = i;
}
/* If no fixed point present
then return -1 */
return index;
}
//main function
答案3
得分: 1
方法在达到return语句时返回给调用它的代码。根据文档:
从方法返回一个值
当方法
- 完成了方法中的所有语句,
- 达到了return语句,或者
- 抛出了异常,
会返回给调用它的代码,以先发生的情况为准。
语句 if(arr[i] == i)
在for
循环的第一步返回true:
i=0
arr[0]=0
然后执行了 return i;
。
在这种情况下,数组的每个值都与它的索引匹配,但在最后的迭代完成后,使用for
循环不会返回4,因为最后的迭代是for
循环的第一次迭代,只针对i=0
。i
的值永远不会达到4。
英文:
> Q: Can someone provide an explanation why using this method below
> returns only the first matching value within the array?
A method returns to the code that invoked it when reaches a return statement.
From doc:
> Returning a Value from a Method
>
> A method returns to the code that invoked it when it
>
> - completes all the statements in the method,
> - reaches a return statement, or
> - throws an exception,
>
> whichever occurs first.
The statement if(arr[i] == i)
returns true at the first step of the for
loop:
i=0
arr[0]=0
Then the line return i;
executed.
> Q: Every value within the array matches to its index 4, should using
> the for loop return 4 after the last iteration is completed?
In this condition the last iteration is the first iteration of the for
loop. It runs only for i=0
. The value of i
never reaches to 4.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论