Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 线性搜索递归最后一次出现_Java_Recursion_Data Structures_Recursive Datastructures_Linear Search - Fatal编程技术网

Java 线性搜索递归最后一次出现

Java 线性搜索递归最后一次出现,java,recursion,data-structures,recursive-datastructures,linear-search,Java,Recursion,Data Structures,Recursive Datastructures,Linear Search,我试图在数组上执行线性搜索,在该数组中找到目标的最后一个匹配项。我被卡住了,因为我的搜索只找到了目标的第一次出现,而不是最后一次 /** Recursive linear search that finds last occurrence of a target in the array not the first * * @param items The array * @param target the item being searched for * @param cur cu

我试图在数组上执行线性搜索,在该数组中找到目标的最后一个匹配项。我被卡住了,因为我的搜索只找到了目标的第一次出现,而不是最后一次

/** Recursive linear search that finds last occurrence of a target in the array not the first
 * 
 * @param items The array
 * @param target the item being searched for
 * @param cur current index
 * @param currentLength The current length of the array
 * @return The position of the last occurrence
 */
public static int lineSearchLast(Object[] items, Object target, int cur, int currentLength){
    if(currentLength == items.length+1)
        return -1;
    else if (target.equals(items[cur])&& cur < currentLength)
        return cur;
    else
        return lineSearchLast(items, target, cur +1, currentLength);
}

public static void main (String[] args){
Integer[] numbers5 = {1,2,4,4,4};
int myResult = lineSearchLast(numbers5, 4, 0, 5);
System.out.println(myResult);
/**递归线性搜索,查找数组中目标的最后一次出现,而不是第一次出现
* 
*@param在数组中添加项
*@param target正在搜索的项目
*@param cur当前索引
*@param currentLength数组的当前长度
*@返回最后一次出现的位置
*/
公共静态int-lineSearchLast(对象[]项,对象目标,int-cur,int-currentLength){
如果(currentLength==items.length+1)
返回-1;
else if(target.equals(items[cur])&&cur
您不应该需要两个索引参数——一个(当前索引)就可以了。此外,为了确保您首先找到最后一个等效元素,谨慎的做法是从后面开始,然后向前推进

/** Returns the index of the last occurance of target in items */
public static int findLastOccurance(Object[] items, Object target){
    return findLastOccurance(items, target, items.length - 1);
}

/** Helper method for findLastOccurance(items, target). Recursively finds
  * the index of the last occurance of target in items[0...cur]
  */
private static int findLastOccurance(Object[] items, Object target, int cur){
    if(curr == -1) //We went past the start - missed it.
        return -1;
    else if (target.equals(items[cur])) //Found it
        return cur;
    else
        return lineSearchLast(items, target, curr-1); //Work backwards
}

它必须是递归的吗?您可以通过迭代列表来简化操作。我建议您返回
max(cur,lineSearchLast(items,target,cur+1,currentLength)
。有
Math#max
,但它返回的是
double
,而不是
int
,因此您可能还想/需要创建或修改此方法。@DanJMiller或通过遍历列表从最后一个元素到第一个元素并返回第一个匹配项。@DanJMiller我正在尝试使用递归方法做得更好,所以我想这样做通过递归实现,虽然我知道有更简单的方法来实现。太棒了,谢谢。首先开始向后搜索是有意义的,这会让事情变得简单很多