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

时间:2014-05-06 10:26:01

标签: java recursion return

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

如果它负责,它会自行返回。

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

这两种情况正常。

如果邻居不负责,请关注调用客户端的第一个邻居,并再次递归调用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 呼叫客户:client0
当前客户端包含元素:false
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 调用元素:client0有这个邻居:
客户3 包含位置:false
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 第一个邻居元素负责:false
执行另一个搜索此客户端正在调用它:client0与它找到的客户端但不是正确的客户端:client3
使用client3调用searchForResponsibleClient 呼叫客户:client3
当前客户端包含元素:false
++++++++++++++++++++++++++++++++++++++++++ 调用元素:client3有这个邻居:
client4
包含位置:true
找到了黄金邻居; client4
!!!!!!达到了我永远无法达到的地位! !!!!!

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

1 个答案:

答案 0 :(得分:1)

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

temporalClient.searchForResponsibleClient(p);

return temporalClient.searchForResponsibleClient(p);

应该做的伎俩。这将解释您为什么要达到您认为不应该到达的代码