gpt4 book ai didi

java - 打印数组列表的内容

转载 作者:太空宇宙 更新时间:2023-11-04 14:26:10 25 4
gpt4 key购买 nike

尝试打印数组列表的内容,但仅获取该级别顺序遍历程序的地址而不是数组列表。如何打印其内容,而不是代码的地址?

  public  static<T> void levelOrder(BinaryTree<T> t) 
{
if(t == null)
return;
// create ArrayList of type Binary Tree
ArrayList<BinaryTree<T>> level = new ArrayList<BinaryTree<T>>();
// add root to the ArrayList
level.add(t);
// while there is at least one node
while(!level.isEmpty())
{
BinaryTree<T> curr = new BinaryTree<T>();
level.add(0,curr);
// add the left node to ArrayList
if(curr.getLeft() != null)
level.add(curr.getLeft());
// add the right node to ArrayList
if(curr.getRight() != null)
level.add(curr.getRight());
// remove element at front of array list
level.remove(0);
}
System.out.println(level);
}

我尝试了下面的 toString 方法,但收到了不兼容的类型错误:void 无法与字符串进行比较

  public String toString(BinaryTree<T> t)
{
return levelOrder(t);
}

这是下面的完整类(class):

      import java.util.ArrayList;
public class BinaryTree<T>
{
private T data;
private BinaryTree<T> parent;
private BinaryTree<T> left;
private BinaryTree<T> right;
// constructor
public BinaryTree()
{
parent = left = right = null;
data = null;
}
// make the root method
public void makeR(T data)
{
if (!isEmpty())
{
System.out.println("Can't make root. Already exists");
}
else
this.data = data;
}
// set and get methods
public void setData(T data)
{
this.data = data;
}
public void setLeft(BinaryTree<T> tree)
{
left = tree;
}
public void setRight(BinaryTree<T> tree)
{
right = tree;
}
public void setParent(BinaryTree<T> tree)
{
parent = tree;
}
public T getData()
{
return data;
}
public BinaryTree<T> getParent()
{
return parent;
}
public BinaryTree<T> getLeft()
{
return left;
}
public BinaryTree<T> getRight()
{
return right;
}
// is Empty method
public boolean isEmpty()
{
if (data == null)
return true;
else
return false;
}
public void attachLeft(BinaryTree<T> tree)
{
if (tree==null) return;
else if (left!=null || tree.getParent()!=null)
{
System.out.println("Can't attach");
return;
}
else
{
tree.setParent(this);
this.setLeft(tree);
}
}
public void attachRight(BinaryTree<T> tree)
{
if (tree==null) return;
else if (right!=null || tree.getParent()!=null)
{
System.out.println("Can't attach");
return;
}
else
{
tree.setParent(this);
this.setRight(tree);
}
}
// Level Order method
public static<T> void levelOrder(BinaryTree<T> t)
{
if(t == null)
return;
// create ArrayList of type Binary Tree
ArrayList<BinaryTree<T>> level = new ArrayList<BinaryTree<T>>();
// add root to the ArrayList
level.add(t);
// while there is at least one node
while(!level.isEmpty())
{
BinaryTree<T> curr = new BinaryTree<T>();
level.add(0,curr);
// add the left node to ArrayList
if(curr.getLeft() != null)
level.add(curr.getLeft());
// add the right node to ArrayList
if(curr.getRight() != null)
level.add(curr.getRight());
// remove element at front of array list
level.remove(0);
}
//I tried a for loop but if I print it outside the while loop, nothing shows.
//If I print it in the while loop, I get infinite loops. Not sure where I should
//place the print statement.
for(int i = 0; i < level.size(); i++)
System.out.println(level.get(i));
}
public void String toString(BinaryTree<T> t)
{
return levelOrder(t);
}

}

最佳答案

对象上的System.out.println将调用该对象的toString方法。

ArrayList 的 toString 方法是一个括号,其中包含每个对象的 toString,

toString的默认实现是对象的地址,您需要做的是实现您在ArrayList中添加的对象的toString方法

关于java - 打印数组列表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26641130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com