如何检查Java中的arraylist树是否为空?
我正在用Java创建一个树。当我创建一个方法来检查树是否为空时,它不能正常工作。当我调试程序,并进入检查root是否为空的if语句时,我一直得到“parent=null”。我认为问题可能是由于父集合方法,但我不确定。这是我的密码:如何检查Java中的arraylist树是否为空?,java,arraylist,tree,Java,Arraylist,Tree,我正在用Java创建一个树。当我创建一个方法来检查树是否为空时,它不能正常工作。当我调试程序,并进入检查root是否为空的if语句时,我一直得到“parent=null”。我认为问题可能是由于父集合方法,但我不确定。这是我的密码: { private Tree data = null; //create a tree private List<GeneralTree> children = new ArrayList<>(); //create an ar
{
private Tree data = null; //create a tree
private List<GeneralTree> children = new ArrayList<>(); //create an arraylist
private GeneralTree parent = null; //create a parent
public GeneralTree(Tree data) //constructor
{
this.data = data;
}
public void addChild(GeneralTree child) //create a child method to create a child
{
child.setParent(this);
this.children.add(child);
}
public void addChild(Tree data) //create a method to put data into the children
{
GeneralTree<Tree> newChild = new GeneralTree<>(data);
this.addChild(newChild);
}
public void addChildren(List<GeneralTree> children) //create a method to add children to a parent
{
for(GeneralTree t: children)
{
t.setParent(this);
}
this.children.addAll(children);
}
public List<GeneralTree> getChildren() //get the children
{
return this.children;
}
public Tree getData() //get the data in the children
{
return data;
}
public void setData(Tree data) //set the data of the children together
{
this.data = data;
}
public void setParent(GeneralTree parent) //set the parent together
{
this.parent = parent;
}
public GeneralTree getParent() //get the parent
{
return this.parent;
}
驱动类的主要方法
publicstaticvoidmain(字符串[]args)
{
GeneralTree root=新建GeneralTree(“根”);//创建根节点
if(root.isEmpty())
{
如果(真)
{
System.out.println(“树是空的”);
}
}
GeneralTree child1=新的GeneralTree(“子1”);//第一个子节点
child1.addChild(“孙子1”);//第一个孙子节点
child1.addChild(“孙子节点2”);//第二个孙子节点
GeneralTree child2=新的GeneralTree(“子节点2”);//第二个子节点
child2.addChild(“孙子3”);//第三个孙子节点
root.addChild(child1);
root.addChild(child2);
root.addChild(“Child 3”);//第三个子节点
root.addChildren(Arrays.asList(新GeneralTree(“子节点4”)、新GeneralTree(“子节点5”)、新GeneralTree(“子节点6”);//添加第四、第五和第六个子节点
for(GeneralTree节点:root.getChildren())//获取并打印子节点,只要它们在根目录下
{
System.out.println(node.getData());//获取数据
}
}
}
我不知道问题是出在父节点还是isEmpty()方法的设计上?您的isEmpty()方法对我来说似乎没有意义
它询问父级是否为空。这和空有什么关系
“父级为空吗?”表示“我是根吗?”
或者换一种说法——根从来没有父对象,因此根据您的测试,root.isEmpty()永远是真的
按照我的想法,“空树”的意思是“没有子对象”(如果可能的话,“这里没有数据)。如果您没有任何
子对象
则数组列表
永远无法实现,那么为什么不将其更改为:
public boolean isEmpty()
{
return children.isEmpty();
}
此外,您还应删除以下条件中的(true)条件:
if(root.isEmpty())
{
if(true)
{
System.out.println("The tree is empty.");
}
}
致:
如果您希望只输出结果(如评论中所示),您还可以执行一些花式java sh**操作,如:
因此,我应该检查父GeneralTree对象是否有子对象?在这种情况下,我还应该创建一个hasChildren()方法来检查这一点?否。x。isEmpty()表示“x是否有子项或数据”。育儿是一件不涉及的事情。我就是这么做的,在主要方法中,我添加了一个if-else语句来检查这一点。然而,这对在场的孩子们不起作用。当我试着和那些没有被评论掉的孩子们在一起时,我仍然得到了“空的”。我做错了什么?``if(root.isEmpty(){System.out.println(“empty”);}else{System.out.println(“notempty”);}我在Java中一直看到“?”标记,但我不知道这是什么意思。您能解释一下吗?在您的代码中,您是在创建根之后(并且在添加任何子项之前)调用该方法的。您能在代码的末尾,或者至少在添加子项之后,放置isEmpty()条件吗?很高兴它帮助了mate:)
public boolean isEmpty()
{
return children.isEmpty();
}
if(root.isEmpty())
{
if(true)
{
System.out.println("The tree is empty.");
}
}
if(root.isEmpty())
System.out.println("The tree is empty.");
System.out.println(root.isEmpty()?"Empty":"Not empty");