简体   繁体   English

如果该方法的参数是先前设置的变量而不是直接输入的,为什么该方法返回不同的值?

[英]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. 您的主要问题是fromto索引 ,但是您从数组中传递了

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. arrayC引用相同的数组。

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 = 0to = 4进行调用。 Both calls are incorrect. 两个电话都不正确。

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

相关问题 为什么这个 output 0.0 0.0 0.0 0.0 而不是 execute 方法的返回计算? - Why does this output 0.0 0.0 0.0 0.0 rather than the execute method's return calculations? 为什么此方法总是将0用作返回值? - Why does this method always produce 0 as its return value? 当返回值不同时,为什么我的排序方法返回 0? - Why does my sort method return 0 when return value is different? 检查数组值是否大于用户输入值的方法 - Method that checks if an array value is greater than a user-inputed value 为什么这个方法打印出347而不是3? - Why does this method print out 347 rather than 3? 为什么我们在java字符串中使用\n而不是直接return key呢? - Why do we use \n rather than return key directly in java string? 当从方法返回值时,首先将该值分配给变量,然后直接返回该变量比直接返回值好? - when return value from a method,assign that value to a variable first and return that variable is better than return value directly? 为什么下面的代码用2而不是1递增值?(Java新手) - Why does the below code increment value with 2 rather than 1?(New to Java) 为什么 BufferedInputStream 将字段复制到局部变量而不是直接使用该字段 - Why does BufferedInputStream copy a field to a local variable rather than use the field directly 不能直接从 Java 中的方法返回值,为什么? - Can't return value directly from method in Java, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM