0

如何使用 java 从文件中添加单词?

4

1 回答 1

0

您只是将其与其中一个孩子进行比较。

for(int i=0; i<rootChildren.length; i++){
                    Node rootChild = rootChildren[i];
                    //see if the addChar1 already exists in the tree
                    //if it doesn't

                    if(!rootChild.equals(addChar1)){
                        //add the addChar1 as a child of the root
                        root.addChild(addChar1);

                    }
                    else{
                        System.out.println(addChar1.getItem() + " Exists in the tree already");
                    } 

在它不等于第一个孩子之后已经添加它。你想看看它是否等于任何孩子。你可以做的是这样的:

  int equalsnrofchildren = 0;
    for(Node rootChild : rootChildren){//loops through all the children
                        //see if the addChar1 already exists in the tree
                        //if it doesn't

                        if(rootChild.equals(addChar1)){
                            equalsnrofchildren++;

                        }
    }

    if(equalsnrofchildren == 0){
    root.addChild(addChar1);
    }else{
System.out.println("already exists..");
}
于 2013-04-04T12:23:28.310 回答