[ACCEPTED]-What is the easiest alternative to a generic array in Java?-generics
The problem is that since the generic type 5 parameter T
is transformed into Object
by the compiler 4 (it's called type erasure), you actually create an array 3 of Object
. What you can do is provide a Class<T>
to the 2 function:
class test<T>
{
private T[] elements;
private int size;
public test(Class<T> type, int size)
{
this.size = size;
elements = (T[]) Array. newInstance(type, size);
}
}
You will find a better exlanation 1 of it here: Angelika Langer - Can I create an array whose component type is a type parameter?
The easiest alternative to a generic array is to 1 use a list.
I tend to avoid basic arrays and use ArrayLists (or 12 similar Lists) wherever possible for a bunch 11 of reasons. The two most important are:
I 10 don't need the semantics of the bald array. There's 9 nothing about the bracket notation (i.e., shorthand 8 for pointer arithmetic) that makes my life 7 easier.
If I use Lists, I'm setting myself 6 up to use more sophisticated concurrency-enabled 5 structures down the line, as needed. For 4 example, it's dead easy to replace an ArrayList 3 with a CopyOnWriteArrayList.
So, writing your example using a 2 generic ArrayList would look something like 1 this:
class test<T> {
/** It's not strictly necessary to make this a List vs. an ArrayList
You decide what interface you really need to expose internally. */
private List<T> elements;
/** This parameter isn't necessary with a List. You can always call the
size() method instead. */
private int size;
public test(int size) {
this.size = size;
elements = new ArrayList<T>();
// Note that the next line isn't strictly necessary.
// An ArrayList can be dynamically resized without any real effort on your part.
elements.ensureCapacity(size);
}
}
There are two solutions that do not require 25 passing in the class object. In both I am 24 assuming that since it's a private variable, you 23 are the only person putting things into 22 it, and the outside world doesn't see it.
One 21 is to just use a plain Object array Object[]
. The 20 disadvantage is that you will lose the benefits 19 of generics and you will have to cast things 18 coming out of it, which makes the code uglier. But 17 since you are the only person putting things 16 into it, then the cast should always be 15 safe. The outside doesn't need to know what's 14 going on.
The other options is kind of a 13 hack. You create an Object[]
, and then "cast" it 12 to T[]
. Technically this is illegal, because 11 Object[]
and T[]
are different runtime types and the 10 former cannot be cast to the latter. But 9 as long as you don't pass the array out 8 of the class (e.g. return it or something), then 7 it won't cause any problems, because T[]
will 6 get erased to Object[]
anyway, and no cast is actually 5 performed. Then you can get the benefits 4 of generics in your class for easier coding, but 3 you have to be extremely careful not to 2 pass that array to anyone who actually expects 1 a T[]
, because it is not.
I may be wrong, but your declaration seems 1 strange, shouldn't you have :
private T[] elements;
The size field does appear redundant. You 3 could just have elements.length instead 2 of size.
class Test<T> {
private final T[] elements;
public test(int size) {
elements = (T[]) new Object[size];
}
}
Or you could replace the Test class 1 with an ArrayList. i.e. drop the class altogether.
Look also to this code:
public static <T> T[] toArray(final List<T> obj) {
if (obj == null || obj.isEmpty()) {
return null;
}
final T t = obj.get(0);
final T[] res = (T[]) Array.newInstance(t.getClass(), obj.size());
for (int i = 0; i < obj.size(); i++) {
res[i] = obj.get(i);
}
return res;
}
It converts a list 2 of any kind of object to an array of the 1 same type.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.