Generic arrays in Java

54,860

Solution 1

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[].

...

Solution 2

The other answers here generally all advocate a better approach for this (especially the recommendation to use an ArrayList instead), but a simple answer in this specific case could be to do:

hashTable = (T[])(new Comparable[tableSize]);

(i.e. create an array of type raw Comparable instead of Object)

If you properly encapsulate all access to this array inside your Hash object this should work, but (as the other answers explain) you could leave yourself vulnerable.

Solution 3

The cast you're attempting

(T[])(new Object[tableSize]);

fails, because the items in the array are instances of Object. Object does not extend Comparable<String>, so the cast (T[]) fails because T is defined as:

T extends Comparable<String>

To resolve this problem either:

  • Instantiate the array so that it's items are instances of some class that does extend Comparable<String>
  • Change hashTable from an Array (which is not a generic type), to a generic collection type, e.g. List<T> hashTable = new ArrayList<T>(tableSize>)

Solution 4

You often run into problems when you need to instantiate something of a generic type. The easiest way to get around this is to pass the class of actually will be stored in on the constructor. This way you can construct from the actual type. Try something like this:

public class Hash<T extends Comparable<String>>
{
  Hash(int records, double load, Class<T> class)
  {
    tableSize = (int)(records / loadFactor);
    tableSize = findNextPrime(tableSize);

    hashTable = java.lang.reflect.Array.newInstance(class, tableSize);
  }

private T[] hashTable;
private int tableSize;

}
Share:
54,860

Related videos on Youtube

Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and &amp; for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on June 22, 2020

Comments

  • Peter Mortensen
    Peter Mortensen almost 4 years

    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?

    • MitMaro
      MitMaro over 14 years
      There are only ugly solutions. IMO one of many problems with Java.
    • pstanton
      pstanton over 14 years
      you mean people who don't know the language ^
    • Has QUIT--Anony-Mousse
      Has QUIT--Anony-Mousse over 11 years
      possible duplicate of Java how to: Generic Array creation
  • pstanton
    pstanton over 14 years
    downvote - see answers using java.lang.reflect.Array.newInstance
  • Admin
    Admin over 14 years
    Thank you very much! This is a great explanation that was more convoluted in other places on the web. Thanks!
  • newacct
    newacct over 12 years
    (Comparable[])Array.newInstance(Comparable.class, tableSize) has the exact same effect as new Comparable[tableSize]
  • Andrew Nguyen
    Andrew Nguyen over 10 years
    A few errors here. List<String> list = new ArrayList<String>(); List<Integer> list2 = (List<Integer>)list; Does not compile. ie at runtime there is no knowledge of T's class You cannot instantiate an instance of T because at compile time you don't know the type of T.
  • mins
    mins about 10 years
    "Arrays are covariant. That means they retain the type of their elements at runtime. Java's generics are not" Not covariant, but reifiable (docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.‌​7)