0

Possible Duplicate:
Java Generics: Array containing generics

I have a Java class which contains 2 methods which add and remove an element from an array. To make it generic it takes a subtype so it should be able to work on different types of objects.

The problem is that when I instantiate it using MapEntry (where MapEntry is an implementation of java.util.Map.Entry) as the subtype. This results in a ClassCastException being thrown when trying to convert an Object array to a MapEntry array. I'm guessing this is because of the following lines (Where T is the subtype):

array = (T[])(new Object[array.length + 1]);
array = (T[])(new Object[array.length - 1]);

Which are used to increase/decrease the array size by 1 respectively. I also use this on arrays of Integers, Strings and Objects, and it works fine with those.

Also, it's explicitly stated that I need to use arrays for this, so no lists, etc.

Is there any way to get around this problem while still keeping the class as generic as possible?

Edit: Managed to get the problem solved. Here's the working code:

array = (T[])Array.newInstance(array.getClass().getComponentType(), array.length + 1);
array = (T[])Array.newInstance(array.getClass().getComponentType(), array.length - 1);

Thanks for all the help :D

Community
  • 1
  • 1
PandaConda
  • 3,146
  • 2
  • 17
  • 22
  • Casting to `T` shouldn't really do anything, at least not if the type parameter is unbound. Could you provide a little more context? The type signature of your class and how you're instantiating it? – millimoose Dec 14 '11 at 11:58
  • That said, you cannot have generic arrays and shouldn't try to do so. I'd use an `Object[]` throughout and just cast when returning values in your methods – it's still an unsafe cast but it's a little more localised. (Or look at how `ArrayList` is implemented and do that.) – millimoose Dec 14 '11 at 12:01

2 Answers2

1

A) don't use arrays, they are awful. Use collections instead.

B) you can't create a generic array without knowing the array type. if you do have the type (the class), you can do:

T[] array = Array.newInstance(type, length);

Read:

Community
  • 1
  • 1
Sean Patrick Floyd
  • 274,607
  • 58
  • 445
  • 566
  • A) As stated in the problem, I'm doing this for an assignment and it explicitly states I have to use arrays :( B) Could you please elaborate on this? If you have to know the array type to create the array, wouldn't this take away the whole point of generics? – PandaConda Dec 14 '11 at 12:17
  • @user1097658 yes, but unfortunately Java generics work that way. Read: http://docs.oracle.com/javase/tutorial/java/generics/erasure.html – Sean Patrick Floyd Dec 14 '11 at 12:37
  • This helped me solve the problem. See my edit in the question. Thanks :P – PandaConda Dec 14 '11 at 14:59
0

A T[] array is a subclass of Object[], and you may the cast T[] to Object[]. But the reverse is false: you can't cast an Object[] to T[] because Object[] is not a subclass of T[].

I think your question boils down to How to create an array of a generic type?

Community
  • 1
  • 1
JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174