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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:37:58+00:00 2026-05-28T04:37:58+00:00

I’m using Java 6. I’m having trouble getting my inner class to use the

  • 0

I’m using Java 6.

I’m having trouble getting my inner class to use the same generic class as its enclosing class. Currently I have

public class TernarySearchTree < T > {
    ...
    protected class TSTNode < T > {
        // index values for accessing relatives array
        protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3; 
        protected char splitchar;
        protected TSTNode < T > [] relatives;
        private T data;

        protected TSTNode(char splitchar, TSTNode < T > parent) {
            this.splitchar = splitchar;
            relatives = new TSTNode[4];
            relatives[PARENT] = parent;
        }
    }
}

Right now I get the warning

The type parameter T is hiding the type T

If I remove the type parameter from the inner class (i.e. remove the <T> from teh protected class TSTNode<T> line), then I get a compile error on the line relatives = new TSTNode[4].

How can I make everything right?

  • 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-28T04:37:59+00:00Added an answer on May 28, 2026 at 4:37 am

    You can either:

    • remove the <T> type parameter from TSTNode (i.e., make it non-generic) – it will still have access to the outer <T>.

    • rename the <T> type parameter in class TSTNode to (say) U.

    [UPDATE]

    Below are four different ways to rewrite your code. All of them compile. I think you should consider the use of an EnumMap (see Version 4, below).

    Version 1: use a differenly named type parameter in the inner class. you need to use a List instead of an array.

      public class TernarySearchTree<T> {
    
        protected class TSTNode<U> {
          // index values for accessing relatives array:
          protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;
    
          protected char splitchar;
          protected List<TSTNode<U>> relatives;
          private U data;
    
          protected TSTNode(char splitchar, TSTNode<U> parent) {
            this.splitchar = splitchar;
            relatives = new ArrayList<TSTNode<U>>();
            for (int i = 0; i < HIKID; ++i) {  // Allocate 4 slots in relatives
              relatives.add(null);
            }
            relatives.set(PARENT, parent);
          }          
        }
    
        private TSTNode<T> node; // When you use it, pass T as U
    
        public TernarySearchTree() {
          node = new TSTNode<T>(',', null);  // When you use it, pass T as U 
        }
      }
    

    Version 2: inherit T from enclosing class

      public class TernarySearchTree<T> {
    
        protected class TSTNode {
          // index values for accessing relatives array:
          protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;
    
          protected char splitchar;
          protected List<TSTNode> relatives;
          private T data;
    
          protected TSTNode(char splitchar, TSTNode parent) {
            this.splitchar = splitchar;
            relatives = new ArrayList<TSTNode>();
            for (int i = 0; i < HIKID; ++i) {  // Allocate 4 slots in relatives
              relatives.add(null);
            }
            relatives.set(PARENT, parent);
          }
        }
    
        private TSTNode node; 
    
        public TernarySearchTree() {
          node = new TSTNode(',', null);  
        }
      }
    

    Version 3: use a Map (instead of a List)

      public class TernarySearchTree<T> {
    
        protected class TSTNode {
          // index values for accessing relatives array:
          protected static final int PARENT = 0, LOKID = 1, EQKID = 2, HIKID = 3;
    
          protected char splitchar;
          protected Map<Integer, TSTNode> relatives;
          private T data;
    
          protected TSTNode(char splitchar, TSTNode parent) {
            this.splitchar = splitchar;
            // Create a hash map. No need to pre-allocate!
            relatives = new HashMap<Integer, TSTNode>(); 
            relatives.put(PARENT, parent); // set -> put
          }
        }
    
        private TSTNode node; 
    
        public TernarySearchTree() {
          node = new TSTNode(',', null);  
        }
      }
    }
    

    Version 4: define the indices as an enum + use an EnunMap (instead of a hash map)

      public class TernarySearchTree<T> {
    
        protected static enum Index {
          PARENT, LOKID, EQKID, HIKID;
        }
    
        protected class TSTNode {    
          protected char splitchar;
          protected EnumMap<Index, TSTNode> relatives;
          private T data;
    
          protected TSTNode(char splitchar, TSTNode parent) {
            this.splitchar = splitchar;
            // Create an EnumMap. 
            relatives = new EnumMap<Index, TSTNode>(Index.class);
            relatives.put(Index.PARENT, parent); 
          }
        }
    
        private TSTNode node; 
    
        public TernarySearchTree() {
          node = new TSTNode(',', null);  
        }
      }
    

    [Update 2]
    One thing to keep in mind: Use EnumMap instead of ordinal indexing

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have thousands of HTML files to process using Groovy/Java and I need to
I want use html5's new tag to play a wav file (currently only supported
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is

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.