0

in java, I want to create an array of class BST witch is a generic object. when I write

box = new BST<T>[n];

it keeps telling me [ Cannot create a generic array of BST ], so I used

box = (BST<T>[]) new Object[n];

the error is gone but when I run it I get ClassCastException

here is the whole implementation

public class hash<T> {
private BST<T> box[];
private int size;

@SuppressWarnings("unchecked")
public hash(int n){
    size = n;
    box = (BST<T>[]) new Object[n];
}
Bilbo Baggins
  • 2,591
  • 7
  • 40
  • 66
  • Casting an Object don't solve the problem. Check this: http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java – Pusker György Jan 11 '15 at 11:14
  • Do you have any super type of BST?? for which you are trying to create generic array?? If not then object array is your only option. It will give you classcastexception because you are trying to convert a raw type to a specific type at runtime while generic works at compile time – Bilbo Baggins Jan 11 '15 at 11:15
  • so how can i solve it, samwise? – Abdullah ALT Jan 11 '15 at 11:27

2 Answers2

2

You can get rid of the runtime exception by writing

box = (BST<T>[]) new BST[n];

There are good reasons why you shouldn't do this though. Arrays and generics don't mix well. Arrays check their type information at runtime. For example, a String[] knows it is an array of String and rigorously maintains this. If you cast a String[] to an Object[] and try to add an Integer you will get a ArrayStoreException at runtime. Generics, on the other hand, are implemented using type erasure, which means that type information exists at compile time but not runtime. If you cast an ArrayList<String> to a raw ArrayList and then add an Integer you will not get any exception at all.

I would advise using a List<BST<T>> instead.

Paul Boddington
  • 35,031
  • 9
  • 56
  • 107
1

There are good reasons for why you are struggling to do what you want. Basically they boil down to the fact that what you want to do will not be type safe. If you managed to create an array of type BST<T>[], it would be an array whose members could have different types (e.g. BST<Integer> and BST<Float>).

You can use an object array and then check the instance type of the member (using instanceof) and cast it when you use it in your program, but that is effectively working around the strong typing of Java, which i wouldn't recommend unless it's absolutely unavoidable in your circumstance. The coding approach would be similar to (though not exactly the same as) the second option described in this answer, which I recommend you have a look at.

This issue is discussed in Oracle's restrictions on generic types. There are similar questions on here (and here), although they are not exactly duplicates as they tend to be asking about using the generic type <T> as the type of the array.

I suggest you use an ArrayList to do what you want. That would seem like the approach that would be more compatible with generics.

Community
  • 1
  • 1
J Richard Snape
  • 18,946
  • 5
  • 47
  • 73