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

  • Home
  • SEARCH
  • 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 465173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:20:58+00:00 2026-05-12T23:20:58+00:00

OK, I’ve been google’ing the web, and I just can’t seem to find any

  • 0

OK, I’ve been google’ing the web, and I just can’t seem to find any solution to my problem. I found lots of solutions, just not any that fit.

I need to create an array of generics. But the generic type itself extends Comparable. When I try the following:

public class Hash<T extends Comparable<String>> {
    private T[] hashTable;
    private int tableSize;

    Hash(int records, double load) {
        tableSize = (int)(records / loadFactor);
        tableSize = findNextPrime(tableSize);
        hashTable = (T[])(new Object[tableSize]);  //Error: Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
    }
}

The problem is that the Object cannot be cast as a generic that extends Comparable. Is there a way around this?

  • 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-12T23:20:59+00:00Added an answer on May 12, 2026 at 11:20 pm

    Generics and arrays don’t mix, basically. The short answer is that you can work around this problem. The longer answer is that you probably shouldn’t and I’ll explain why.

    You could use Array.newInstance() like this:

    private Comparable[] hashtable;
    
    ...
    
    hashtable = (Comparable[])Array.newInstance(Comparable.class, tableSize);
    

    but you can’t create an array of your parameterized type.

    Arrays are covariant. That means they retain the type of their elements at runtime. Java’s generics are not. They use type erasure to basically mask the implicit casting that is going on. It’s important to understand that.

    So when you create an Object array you can’t cast it to, say, a Comparable array (or any other type) because that is not correct.

    To give you an example. With generics this is perfectly legal:

    List<String> list = new ArrayList<String>();
    List<Integer> list2 = (List<Integer>)list;
    list.add(3);
    

    It’s also why you can’t do this:

    public <T> T newInstance(T t) {
      return new T(); // error!
    }
    

    ie at runtime there is no knowledge of T’s class. This is why the above code is more often written as:

    public <T> T newInstance(T t, Class<T> clazz) {
      return clazz.newInstance();
    }
    

    because their is no runtime type for the generic argument. But with arrays:

    String arr[] = new String[10];
    Integer arr2[] = (Integer[])arr; // error!
    

    What you should be doing in this case (imho) is not using arrays but using an ArrayList. In all honesty, there is very little reason to use arrays over an ArrayList and generics is just one example of that.

    For a better and more complete explanation see the (excellent) Java Generics FAQ:

    Can I create an array whose component type is a concrete parameterized type?

    No, because it is not type-safe.

    Arrays are covariant, which means that
    an array of supertype references is a
    supertype of an array of subtype
    references. That is, Object[] is a
    supertype of String[] and a string
    array can be accessed through a
    reference variable of type Object[].

    …

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have been unable to fix a problem with Java Unicode and encoding. The
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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 find ID3V2 tags from MP3 file using jid3lib in Java.
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was

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.