打印阵列列表的内容

时间:2014-10-29 21:39:20

标签: java arraylist binary-tree

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

  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);
  }

这是下面的完整课程:

      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);
}

}

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

使用此代码代替System.out.println(level);

ListIterator<Object> li=level.ListIterator();

while(li.hasNext()){
 System.out.println(((BinaryTree<T>)li.next()));

}

1)你需要覆盖T引用的类中的toString()方法(如果T引用了Primitive Wrapper Classes,那么就不需要覆盖toString())。  2)还覆盖BinaryTree类中的toString()以根据您的问题显示有意义的值