[英]Why does the method return a different value if its arguments are previously set variables rather than inputed directly?
Below is the relevant part of my program. 以下是我程序的相关部分。
Relevant part of main method 主要方法的相关部分
int[] C = createIntArray_descending();
int found, target, from, to;
int[] array;
array = C;
target = 5;
from = array[0];
to = array[array.length-1];
printArray(array);
bubble_sort(array);
printArray(array);
found = searchAscending(target, array, from, to);
found = searchAscending(5, C, C[0], C[C.length-1]);
System.out.println(found);
Method searchAscending(), a binary search method 方法searchAscending(),二进制搜索方法
public static int searchAscending(int target, int[] array, int from, int to) {
int mid = (from + to)/2;
if (to<from)
return -1;
else if (target == array[mid])
return mid;
else if (to == from)
return -1;
else if (target < array[mid])
return searchAscending(target, array, from, mid-1);
else
return searchAscending(target, array, mid+1, to);
}
When I run the program using 当我使用运行程序时
found = searchAscending(5, C, C[0], C[C.length-1]);
the method returns 4, meaning that 5 is in fact in the array C, found at position 4. I then wanted to generalise it for other arrays, so I created variables target, from, to and an empty int array, which I could then set as whatever array I wanted to apply the method to, using 该方法返回4,这意味着实际上在位置4的数组C中有5。我然后想将其推广到其他数组,因此我创建了变量target,from,to和一个空的int数组,然后我可以设置为我想将方法应用于的任何数组,使用
found = searchAscending(target, array, from, to);
However when I run the program using this line rather than the previous one, the method returns -1 (meaning it didn't find the target, 5, in the array). 但是,当我使用这一行而不是上一行运行程序时,该方法返回-1(表示它未在数组中找到目标5)。
Why are they returning different values? 他们为什么返回不同的值? They seem to be essentially the same to me. 在我看来,它们基本上是相同的。
EDIT: 编辑:
I figured out that the problem comes from the variables from and to, the the line found = searchAscending(target, array, array[0], array[array.length-1])
works, so the values are successfully being passed, but (target, array, from, array[array.length-1])
gives an ArrayIndexOutOfBoundsException.... 我发现问题出在往返的变量, found = searchAscending(target, array, array[0], array[array.length-1])
的行found = searchAscending(target, array, array[0], array[array.length-1])
有效,因此值已成功传递,但是(target, array, from, array[array.length-1])
给出了ArrayIndexOutOfBoundsException...。
So I get the same answer no matter which way I call it. 因此,无论用哪种方式称呼,我都会得到相同的答案。 However I did notice a problem with how you call your binary search function, searchAscending. 但是我确实注意到您如何调用二进制搜索功能searchAscending出现问题。 For to and from you all calling it using the value at the index, rather than the index itself. 对于所有人,您都使用索引处的值而不是索引本身来调用它。 Try this for your main: 尝试以下主要方法:
int[] C = createIntArray_descending();
int found, target, from, to;
int[] array;
array = C;
target = 5;
from = 0;
to = array.length-1;
printArray(array);
bubble_sort(array);
printArray(array);
found = searchAscending(target, array, from, to);
System.out.println(found);
found = searchAscending(5, C, 0, C.length-1);
System.out.println(found);
First, I'm going to assume createIntArray_descending()
creates array {5,4,3,2,1}
, and bubble_sort(array)
correctly sorts that to {1,2,3,4,5}
. 首先,我假设createIntArray_descending()
创建数组{5,4,3,2,1}
, bubble_sort(array)
正确排序为{1,2,3,4,5}
。
Your main problem is that from
and to
are indexes , but you pass in values from the array. 您的主要问题是from
和to
是索引 ,但是您从数组中传递了值 。
Also note that array = C
is meaningless to your code, because it does not copy the array. 还要注意的是array = C
是毫无意义的代码,因为它不数组复制。 array
and C
are referencing the same array. array
和C
引用相同的数组。
To explain why it doesn't work, use a debugger and examine the values of your code: 要解释为什么它不起作用,请使用调试器并检查代码的值:
int[] C = createIntArray_descending(); // C = {5,4,3,2,1}
array = C; // array = {5,4,3,2,1}
target = 5;
from = array[0]; // from = 5
to = array[array.length-1]; // to = 1
printArray(array);
bubble_sort(array); // array = C = {1,2,3,4,5}
printArray(array);
found = searchAscending(target, array, from, to); // (5, C, 5, 1) -> found = -1
found = searchAscending(5, C, C[0], C[C.length-1]); // (5, C, 1, 5) -> found = 4
System.out.println(found);
In both calls to searchAscending()
, you were supposed to call with from = 0
and to = 4
. 在对searchAscending()
两次调用中,应该以from = 0
和to = 4
进行调用。 Both calls are incorrect. 两个电话都不正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.