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

2
  • 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? Commented Dec 14, 2011 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.) Commented Dec 14, 2011 at 12:01

2 Answers 2

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:

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@user1097658 yes, but unfortunately Java generics work that way. Read: docs.oracle.com/javase/tutorial/java/generics/erasure.html
This helped me solve the problem. See my edit in the question. Thanks :P
@SeanPatrickFloyd Regarding point A, if only we could easily pass a collection into a varargs parameter without converting it into an array, I would be happy to comply =) (I'm looking at you, Map.ofEntries() )
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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.