如何处理这个不会构造的迭代器类型的对象?

How to deal with this object of type iterator that won't construct?

我无法在 eclipse 中编译以下行:

Iterator<T> allElementsInTree = theTree.iteratorLevelOrder();

.iteratorLevelOrder(); returns 一个 Iterator<T> 类型的对象,但 Eclipse 告诉我

T cannot be resolved to a type

作为一个错误。有人知道这是为什么吗?

编辑:出于某种原因,它以 Iterator<String> 运行,但它在运行时崩溃,显示

invalid cast error

这是方法的代码:

public Iterator<T> iteratorLevelOrder() 
       {

          ArrayUnorderedList<T> nodes = new ArrayUnorderedList<T>();
          ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
          BinaryTreeNode<T> current;

          nodes.addToRear (root.element);

          while (! nodes.isEmpty()) 
         {
             current = (BinaryTreeNode<T>)nodes.removeFirst();

             if (current != null) 
            {
                templist.addToRear(current.element);
                nodes.addToRear (current.left.element);
                nodes.addToRear (current.right.element);
             }//if
             else
                templist.addToRear(null);
          }//while
          return templist.iterator();
       }  // method levelorder

这是主要方法。

public static void main(String[] args) {
        LinkedBinaryTree<String> theTree = null; //theTree built here.
        TreeBuilder<String> theTreeBuilder = null;

        try{
            theTreeBuilder = new TreeBuilder<String>(args[0]);
            theTree = theTreeBuilder.buildTree();
        }
        catch( MalformedTreeFileException e) {
            System.out.println( e.getMessage());
        }
        catch( IOException e){
            System.out.println("Error reading file: " + args[0] + "\n" + e.getMessage());
        }

和 LinkedBinaryTree:

public class LinkedBinaryTree<T> implements BinaryTreeADT<T>

{

protected int count;
protected BinaryTreeNode<T> root; 

/**
 * Creates an empty binary tree.
 */
public LinkedBinaryTree() 
{
    count = 0;
    root = null;
}

/**
 * Creates a binary tree with the specified element as its root.
 *
 * @param element  the element that will become the root of the new binary
 *                 tree
 */
public LinkedBinaryTree (T element) 
{
    count = 1;
    root = new BinaryTreeNode<T> (element);
}

/**
 * Returns a reference to the element at the root
 *
 * @return                           a reference to the specified target
 * @throws EmptyCollectionException  if the tree is empty
 */
public T getRoot() throws EmptyCollectionException
{
    return root.element;
}

/**
 * Returns true if this binary tree is empty and false otherwise.
 *
 * @return  true if this binary tree is empty
 */
public boolean isEmpty() 
{
    return count==0; 
}

/**
 * Returns the integer size of this tree.
 *
 * @return  the integer size of this tree
 */
public int size() 
{
    return count;
}


/**
 * Performs an inorder traversal on this binary tree by calling an
 * overloaded, recursive inorder method that starts with
 * the root.
 *
 * @return  an in order iterator over this binary tree
 */
public Iterator<T> iteratorInOrder()
{
    ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
    inorder (root, tempList);

    return tempList.iterator();
}

/**
 * Performs a recursive inorder traversal.
 *
 * @param node      the node to be used as the root for this traversal
 * @param tempList  the temporary list for use in this traversal
 */
protected void inorder (BinaryTreeNode<T> node, 
        ArrayUnorderedList<T> tempList) 
{
    if (node != null)
    {
        inorder (node.left, tempList);
        tempList.addToRear(node.element);
        inorder (node.right, tempList);
    }
}

/**
 * Performs an preorder traversal on this binary tree by calling 
 * an overloaded, recursive preorder method that starts with
 * the root.
 *
 * @return  an pre order iterator over this tree
 */
//public Iterator<T> iteratorPreOrder() 
//{
    //left as programming project 
//}

/**
 * Performs a recursive preorder traversal.
 *
 * @param node  the node to be used as the root for this traversal
 * @param tempList  the temporary list for use in this traversal
 */
protected void preorder (BinaryTreeNode<T> node, ArrayUnorderedList<T> templist) 
   {

      if (node != null) 
     {
         templist.addToRear(node.element);
         preorder (node.left, templist);
         preorder (node.right, templist);
      }//if

   }  // method preorder
/**
 * Performs an postorder traversal on this binary tree by calling
 * an overloaded, recursive postorder method that starts
 * with the root.
 *
 * @return  a post order iterator over this tree
 */

public Iterator<T> iteratorPreOrder() 
   {
      ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
      preorder (root, templist);
      return templist.iterator();
   }  // method preorder


public Iterator<T> iteratorPostOrder() 
   {
      ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
      postorder (root, templist);
      return templist.iterator();
   }  // method postorder

/**
 * Performs a recursive postorder traversal.
 *
 * @param node      the node to be used as the root for this traversal
 * @param tempList  the temporary list for use in this traversal
 */
protected void postorder (BinaryTreeNode<T> node, ArrayUnorderedList<T> templist) 
   {

      if (node != null) 
     {
         postorder (node.left, templist);
         postorder (node.right, templist);
         templist.addToRear(node.element);
      }//if

   }  // method postorder

/**
 * Performs a levelorder traversal on this binary tree, using a
 * templist.
 *
 * @return  a levelorder iterator over this binary tree
 */
public Iterator<T> iteratorLevelOrder() 
   {

      ArrayUnorderedList<T> nodes = new ArrayUnorderedList<T>();
      ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
      BinaryTreeNode<T> current;

      nodes.addToRear (root.element);

      while (! nodes.isEmpty()) 
     {
         current = (BinaryTreeNode<T>)nodes.removeFirst();

         if (current != null) 
        {
            templist.addToRear(current.element);
            nodes.addToRear (current.left.element);
            nodes.addToRear (current.right.element);
         }//if
         else
            templist.addToRear(null);
      }//while
      return templist.iterator();
   }  // method levelorder

Iterator allElementsInTree = theTree.iteratorLevelOrder();

使用您提供的类型(整数、字符串或自定义对象)创建 theTree class.

  Iterator<Integer> allElementsInTree = theTree.iteratorLevelOrder();

或者

Iterator<BinaryTreeDataModel> allElementsInTree = theTree.iteratorLevelOrder();