Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:39:59+00:00 2026-05-14T02:39:59+00:00

So I have a Tree<E> class where E is the datatype held and organized

  • 0

So I have a Tree<E> class where E is the datatype held and organized by the tree. I’d like to iterate through the Tree like this, or in a way similar to this:

1.  Tree<String> tree=new Tree<String>();
2.  ...add some nodes...
3.  for (String s : tree)
4.      System.out.println(s);

It gives me an error on line 3 though.

Incompatible types
    required: java.lang.String
    found:    java.lang.Object    

The following however works fine and as expected, performing a proper in-order traversal of the tree and printing each node out as it should:

for (TreeIterator<String> i = tree.iterator(); i.hasNext(); )
    System.out.println(i.next());

My Tree class looks like this:

public class Tree<E> implements java.lang.Iterable{
    ...
    public TreeIterator<E> iterator(){
        return new TreeIterator<E>(root);//return an iterator for the root node
    }
    ....
}

And my TreeIterator class looks like this:

public class TreeIterator<E> implements java.util.Iterator<E>{
    public E next(){
        ...
    }
    ...
}

But I want to get the for (String s : tree) loop working properly – any ideas? The whole point of this was to set up a clean foreach loop for use in my program, not to use that ugly for loop.

Any idea what I’m doing wrong?


Edit:

As per the best answer (and another equally good answer which was posted shortly after), the following made it work:

Changing

public class Tree<E> implements java.lang.Iterable{
    ....
}

to

public class Tree<E> implements java.lang.Iterable<E>{
    ....
}

…Thanks guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:40:00+00:00Added an answer on May 14, 2026 at 2:40 am

    The foreach loop should be working fine if your Tree<E> class also implements the Iterable<E> interface. You need to make sure that your Iterator also returns the generic type E.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
So suppose I have a tree class like this in c++ class Node{ void
I have a tree of active record objects, something like: class Part < ActiveRecord::Base
I have tree control object created using CTreeCtrl MFC class. The tree control needs
I have this Task model: class Task < ActiveRecord::Base acts_as_tree :order => 'sort_order' end
I have a tree structure in memory that I would like to render in
I have a business object project, which contains composite structure: public class Tree {
I have a tree-like structure of abstract classes and case classes representing an Abstract
I have a webservice that returns a complex value (this is a tree) public
I have this simple ASP.NET page here: <%@ Page Language=C# AutoEventWireup=true CodeBehind=Tree.aspx.cs Inherits=CoconutTree.One %>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.