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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:03:29+00:00 2026-06-07T10:03:29+00:00

I need to create a TreeNode class, that will be able to store childs

  • 0

I need to create a TreeNode class, that will be able to store childs of two types: String and TreeNode. Number of childs is not fixed.

I want to create TreeNode objects somehow like this:

TreeNode a = new TreeNode("str", new TreeNode("str2"), "str3"); //Correct
TreeNode b = new TreeNode(a, "str4); //Correct
TreeNode c = new TreeNode(54); //Wrong

How can I do arguments type checking with wildcards or something else in compile time?

My inappropriate runtime solution:

private static final boolean debug = "true".equals(System.getProperty("debug"));

public <T> TreeNode (T... childs) {
    if (debug) {
        for (Object child : childs) {
            if (!(child instanceof String || child instanceof TreeNode)) {
                throw new RuntimeException("Type of childs must me Tree or String");
            }
        }
    }
}
  • 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-07T10:03:30+00:00Added an answer on June 7, 2026 at 10:03 am

    Parameters in constructor should have special meaning. Using varargs there is acceptable but it think that those are special cases. And you problem can be solved in another way.

    public class TreeNode {
    
       public TreeNode() {
         //crate the object
       }
    
       public TreeNode addNode(String node, String... nodes) {
        //Do something with string node
        return this;
       }
    
       public TreeNode addNode(TreeNode node, TreeNode... nodes) {
       //Do something with TreeNode
        return this;
       }
    }
    

    So you could use this like this for example

    TreeNode node = new TreeNode().addNode("One","Two").addNode(node3,node4);
    

    where node3 and node4 are instaces of TreeNode;

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

Sidebar

Related Questions

I create service and I need create class MyErrors (not exception) My web method
I need to create a script that will log into an authenticated page and
I need to extend the TreeNode class such that I can add custom properties
I have class named config that have private string variable named param. I need
I need create clone repository. but I do not know where can I get
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
Need to create a custom DNS name server using C which will check against
I need to create a keyed hash for a string of XML to send
I need create a table to store Black listed IP address in case of
I need create custom tabbar with super class UITabBarController. But i don,t know how

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.