1

こんにちは私は試行によって辞書を実装する必要があるプロジェクトを持っています...しかし今私は検索メソッドを実装することができません....私のコードはここにあります

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ステートメントにエラーがあります!!!! ルートを子の1つに置き換えたいのですが、その値はkey(temp)の最初の文字に似ていますが、「elseステートメント」ではこれを実行できません...そしてなぜ値にアクセスできないのですか?子どもたちの??

4

2 に答える 2

0

Ok, root is TriesNode type, but root.children is not the same type, that's the problem. You can't assign values from different types. You must declare a variable of root.children type and then assign that value. To assign directly the value of root.children to root you must do:

root.Add(root.children)

more or less...

于 2011-04-20T07:17:16.363 に答える
0

root = root.children.value.equals(temp) root.child を root に割り当てず、temp と等しいかどうかをチェックするため、true または false を root に割り当てます。

また、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       
}      
于 2011-04-20T07:19:15.647 に答える