如何使用java从文件中添加单词?
答案 0 :(得分: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..");
}