当前位置: 首页 > 知识库问答 >
问题:

调用自身的递归搜索方法返回错误的值

丰飞龙
2023-03-14

我正在编写一个在简单的对等网络中搜索客户端的方法。我编写的方法 searchForResponsibleClient 在此网络中获取一个点,并检查调用 searchForResponsibleClient 方法的客户端是否负责此点。

如果它有责任,它会自己回来。

如果它不负责,它会查看其客户端邻居(保存在对象中),并检查是否有任何邻居负责,如果是这样,它将返回邻居。

这两个场景工作正常。

如果邻居不负责,则采用howewer,即调用客户端的第一个邻居,并以递归方式再次调用searchForResponsibleClient方法。

当我以递归方式调用它时,我在控制台上获得了正确的输出,但返回值是错误的。

这是我的代码:

public ClientInterface searchForResponsibleClient(Position p) {
    System.out.println("calling searchForResponsibleClient with " + this.uniqueID);


    boolean contains =  this.clientArea.contains(p);
    System.out.println("calling client: "+ this.uniqueID);
    System.out.println("The current client contains the element:"+ contains);

    // the current client contains the document
    if (contains){
        System.out.println("current element is responsible" +this.uniqueID);
        return this;
    }

    // apparently the current client is not responsible lets check the clients neighbours.
    System.out.println("++++++++++++++++++++++++++++++++++++++++++");
    System.out.println("calling element: "+ this.uniqueID + " has this neighbours:");
    for(ClientInterface neighbour: this.neighbours){
        System.out.println(neighbour.getUniqueID());
        System.out.println("contains the position : "+neighbour.getArea().contains(p));
        if(neighbour.getArea().contains(p)){
            System.out.println("found golden neighbour; "+neighbour.getUniqueID());
            return neighbour;
        }
    }

   System.out.println("+++++++++++++++++++++++++++++++++++++++++++");


   // if the neighbours are not responsible lets get the first neighbour of the neighbourlist and restart the search
   ClientInterface temporalClient = this.neighbours.get(0);
   System.out.println("the first neighbour element is responsible: "+ temporalClient.getArea().contains(p));

   if (!temporalClient.getArea().contains(p)){
        System.out.println("Performing another search this client is callling it: "+ this.uniqueID +" with the client that it found but was not the right one: "+ temporalClient.getUniqueID());
        temporalClient.searchForResponsibleClient(p);
   }
   else {
       return temporalClient;
    }
  System.out.println("!!!!!! reached the position that i should never reach! !!!!!");
  return null;
}

这是我的控制台的输出:

使用client0调用searchForResponsibleClient < br >调用客户端:client0
当前客户端包含元素:false

调用元素:client0具有此邻居:< br> client3
包含位置:false

第一个邻居元素负责:false
执行另一个搜索此客户端正在使用它找到但不是正确的客户端调用它:client 0:client3 < br >使用client 3调用searchForResponsibleClient < br >调用客户端:client3
当前客户端4
!达到了我永远不应该达到的位置!< br >

在这种情况下,client4应该包含位置(实际上就是这种情况),但返回的不是client4 null,这会导致NullpointerException。我一定在我的返回语句中的某个地方犯了错误,但不知何故,我只是看不出错误可能在哪里。

共有1个答案

孔鸿远
2023-03-14

您需要返回找到的最终值。它看起来像修改这行:

temporalClient.searchForResponsibleClient(p);

return temporalClient.searchForResponsibleClient(p);

应该做这个把戏。这将解释为什么你到达了你认为不应该到达的代码。

 类似资料:
  • 我现在正在学习Java,有一个任务,我应该创建一个方法,在输入两个关键字中的一个之前,一直要求用户输入。然后,它应该返回一个int。

  • 我正在ApacheSpark上的数据库中构建一个族谱,使用递归搜索来查找数据库中每个人的最终父级(即族谱顶部的人)。 假设搜索id时返回的第一个人是正确的家长 它给出以下错误 “原因:org.apache.spark.SparkException:RDD转换和操作只能由驱动程序调用,不能在其他转换中调用;例如,

  • 首先,这是家庭作业,所以把它放在外面。 我应该用特定的方法实现二叉查找树: void insert(字符串)、boolean remove(字符串)和boolean find(字符串)。 我已经能够成功地编程和测试插入,并找到方法,但我有困难与删除。 我的程序中发生的事情是,删除实际上并没有从树中删除任何东西,我相信这是因为它只引用当前节点的本地创建,但我可能错了。我认为我可以实现我需要测试的不同

  • 我目前正在编码一个二叉查找树,目前正在尝试实现一个递归函数来确定二叉树中是否存在一个节点。 这是节点类: 所讨论的函数是Exists函数。这是在BST的根节点上调用的,就像这样:

  • 我写了一个二分搜索的递归程序,正如你所看到的,我试图在给定的数组中找到目标=21的位置,然后返回位置为2。但是,我的输出是1。当我调试它匹配att arr[start]=target时,它直接跳到findTheNumber(arr,mid+1,end,target)行;然后下一行,然后返回mid..只是想知道为什么我的返回在“返回开始”时中断了 }