虽然循环保证返回一个值,什么应该替换return语句?

时间:2014-04-16 06:29:36

标签: java return return-value

以下代码应返回与输入数字最接近的值' x'在排序的数组中。 while循环保证返回。但编译器可以理解地迫使我返回一个值。我的问题是涉及最佳实践。在编译器强制使用return语句的情况下,使用的一般约定是什么?即抛出异常?返回默认值?等?

NOTE: I have selected "IllegalArgumentException" http://codereview.stackexchange.com/questions/47328/find-the-value-nearest-to-k-in-the-sorted-array. I dont have any answer to select, but Thank all for insight. I am not aware about SO protocols in such cases

/**
 * Given a sorted array returns the 'closest' element to the input 'x'.
 * 'closest' is defined as Math.min(Math.abs(x), Math.abs(y)) 
 * 
 * Expects a sorted array, and if array is not sorted then results are unpredictable.
 * 
 * @param a  The sorted array a
 * @return   The nearest element
 */
public static int getClosestK(int[] a, int x) {

    int low = 0;
    int high = a.length - 1;

    while (low <= high) {
        int mid = (low + high) / 2;

        // test lower case
        if (mid == 0) {
            if (a.length == 1) {
                return a[0];
            }

            return Math.min(Math.abs(a[0] - x), Math.abs(a[1] - x)) + x;
        }


        // test upper case
        if (mid == a.length - 1) {
            return a[a.length - 1];
        }

        // test equality
        if (a[mid] == x || a[mid + 1] == x) {
            return x;
        }


        // test perfect range.
        if (a[mid] < x  && a[mid + 1] > x) {
            return Math.min(Math.abs(a[mid] - x), Math.abs(a[mid + 1] - x)) + x;
        }

        // keep searching.
        if (a[mid] < x) {
            low = mid + 1;
        } else { 
            high = mid;
        }
    }

    //  return a[0]; ?????
}

7 个答案:

答案 0 :(得分:3)

如果您知道 将返回,但编译器无法确定它,这是程序员错误,因此您可以允许自己::

throw new IllegalStateException();

而不是返回一个错误的值。

答案 1 :(得分:2)

a是一个空数组时怎么办?然后会发生以下情况:

public static int getClosestK(int[] a, int x) {
    int low = 0;
    int high = a.length - 1; // 0 - 1 = -1

    while (low <= high) { // 0 <= -1 -> false
        // ...
    }
    // There is no return here. What should happen?
}

这就是编译器强迫你返回的原因。

答案 2 :(得分:2)

编译器强制你返回值,因为你可能永远不会满足while循环的条件,因此你不能进入while循环并返回值。

在这种情况下,您将无法获得应该返回int的方法的return语句。

在您的情况下,如果int[] a的长度为0,则您不会返回任何内容,因为您不满足while循环的条件。

在这种情况下,您需要validate the inputif input does not pass validation check, you can return an exception。在这里,IllegalArgumentException似乎适合我,但它完全取决于你想要抛出哪个异常。

throw new IllegalArgumentException("Error message");

答案 3 :(得分:1)

如果您确定循环无法正常结束,则可以使用普通while( true )替换循环条件而不更改代码语义。

一般情况下,如果您确定某些事情不会发生,请在此时抛出AssertionError,并在上面写下解释&#34;不可能的条件&#34;的消息。在你的情况下,它可能像

throw new AssertionError("not reachable");

AssertionError更适合标记&#34;不可能&#34;条件,而IllegalStateException是系统状态不允许执行某些其他有效操作的情况的适当例外。

答案 4 :(得分:1)

你遇到的问题是你的while循环保证了返回,但是永远不会输入。

初始化值为

int low  = 0;
int high = a.length - 1;

如果你传递一个空数组,你将得到

while(0 <= -1) {
 ...
}

作为@fge,建议您可以抛出有关非法国家的例外情况。

答案 5 :(得分:0)

你可以抛出一个RuntimeException,它是Java语言规范中的例子:this

  

如果声明某个方法具有返回类型,则每次返回   声明(第14.17节)在其正文中必须有一个表达式。编译时   如果方法的主体可以正常完成,则会发生错误(第14.1节)。       换句话说,具有返回类型的方法必须仅使用提供值返回的return语句返回;它不是   允许“从身体的末端掉下来”。       请注意,方法可能具有声明的返回类型,但不包含return语句。这是一个例子:

     class DizzyDean {
        int pitch() { throw new RuntimeException("90 mph?!"); }
    }

答案 6 :(得分:0)

您有两种选择:

  • 返回一些“虚拟”值
  • 抛出异常

我建议你使用异常(一些运行时异常 - IllegalStateException可能会很好),因为返回“error”值看起来更像是C风格的编码。

相关问题