Trying to find the index if the array is in range

时间:2017-06-15 09:36:07

标签: java arrays search

Here is the question :

Complete the method findInRange that returns the first index at which an item in the range [low, high] (including low and high) exists in the array arr. Return -1 if the array is null or if no such item exists.

For example, the first item in the range [30, 50] is at index 2 in the array {80, 60, 40, 30, 30, 90, 50}.

public int findInRange(int[] arr , int low , int high) {
    if (arr == null){
        return -1;
    }

    for( int i = 0; i < arr.length; i++){
        if( arr[i] >= low && arr[i] <= high){ 
            return i;
        }
    }
}

}

However it seems there is any issue as it says : this method must return a result of type int. It says that in the first line of code.

Edit: So the answer is to add a default return value/

0 个答案:

没有答案