在Java中进行尝试搜索

| 嗨,我有一个项目,我需要通过尝试实现字典...但现在我无法实现搜索方法....我的代码在这里
public class TriesNode {
String value;
ArrayList<TriesNode> children = new ArrayList<TriesNode>();


String findNode(TriesNode root , String key ){
    for (int i=0 ; i<key.length() ; ++i){
        char temp= key.charAt(i);
        if ( !(root.children.equals(temp)))
            return null;
        else
            root = root.children.value.equals(temp);
    }
}
在这段代码中,我在else语句中有错误! 我想用一个孩子替换它的根,它的值类似于key(​​temp)的第一个字符,但是我不能在“ else statement”中这样做...以及为什么我可以无法获得儿童的价值?
已邀请:
好的,root是TriesNode类型,但是root.children不是同一类型,这就是问题所在。您不能分配不同类型的值。您必须声明一个root.children类型的变量,然后分配该值。要将root.children的值直接分配给root,您必须执行以下操作:
root.Add(root.children)
或多或少...
root = root.children.value.equals(temp)不会将root.child分配给root,而是将true或false分配给root,因为您需要检查它是否等于temp。 Java也不允许您使用if语句,它从if语句返回不同类型的值。 这将返回链中的最终根,这是您寻找的值吗? 尝试
        TriesNode findFinalRoot(TriesNode root, String key){
                      if(key.length() == 0 )
              return root;
        for(int x = 0 ; x <root.children.lenth(); x++)

          if (key.charAt(0) == root.children.get(x).charAt(0)){
             findFinalRoot(root,key.subString(1)); // here you loss first character       
}      

要回复问题请先登录注册