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 9316449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:37:18+00:00 2026-06-19T02:37:18+00:00

I have a large data structure that looks like a tree. I have a

  • 0

I have a large data structure that looks like a tree. I have a root class TreeElement that implements all the tree behaviors, linking, unlinking, navigating, etc. Actual nodes within the tree will be specialized subclasses of the TreeElement class. A given parent node, will have more than one type of child. Here is a sample tree:

NodeTypeA
+ NodeTypeB
  + NodeTypeC
  + NodeTypeC
+ NodeTypeB
  + NodeTypeC
+ NodeTypeD
  + NodeTypeE

As you can see, NodeTypeA has children of types B and D (and possibly others), while NodeTypeB has children of C (and possibly others). NodeTypeD has children of type E (and possibly others). All of these are subclasses of TreeElement. The specialized subclasses have some knowledge of the kinds of children that they are allowed to have.

The base type TreeElement has a method

Vector getChildren(String kind, Class nodeType);

This returns a vector filled with all the children marked with the value of kind, and of class specified with the class passed in. The vector returned will always contain elements of the class passed in. The problem is that I am trying to implement the right kind of type safety, and it is not clear to me how to use the dynamic typing capabilities of java for this.

In the class NodeTypeA, I would like a method like this that gets children of NodeTypeB:

Vector<NodeTypeB> getSpecialChildren() {
    Vector<NodeTypeB> vb = getChildren("special", NodeTypeB.class);
    return vb;
}

When I write the above, I get a warning in Eclipse that a type cast is needed. So I can add a type cast:

Vector<NodeTypeB> getSpecialChildren() {
    Vector<NodeTypeB> vb = (Vector<NodeTypeB>) getChildren("special", NodeTypeB.class);
    return vb;
}

But, I also have the feature of Eclipse to “remove unnecessary casts” and this feature removes this cast. That seems kind of strange that Eclipse considers it unnecessary, but warns if it is not there. But that is just an annoyance.

What I really want to do is to use the dynamic type mechanism (is this the ‘bounded type parameters’???) of Java to make this work right. I simply can’t figure out how to declare the getChildren method in the TreeElement class to accomplish this mechanism with full type safety — and I assume eliminating the class parameter. Any help would be appreciated.

  • 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-06-19T02:37:20+00:00Added an answer on June 19, 2026 at 2:37 am

    The way to fix the declaration of getChildren is:

    public <T extends TreeElement> Vector<T> getChildren(String string, Class<T> nodeType)
    

    That will get the compiler to leave you alone with the type warnings when you use.

    The implementation of that method will still have some warnings in it, since generics are a compile-time feature and you’re supplying a run-time type argument, but the warnings will at least be isolated inside that method.

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

Sidebar

Related Questions

I have a data-structure (in plist) that looks something like this: What i have
I have a large tree-like data structure of objects which behave mostly identical but
I have a large codebase (written by me) that uses the Stack data structure.
I have a couple hundred text files that have data that looks like: Capo:
I have a large data.frame displaying some weird properties when plotted. I'd like to
I have a large data set that I'm working with in excel. About 1000+
I have a large set of data that is generated from a web service
I have a large table of data (that displays test data) that is separated
I have a structure called Patch that represents a 2D array of data. newtype
So I have a Python file that contains a large set of data of

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.