oleber
asked on
Create an Array of generic Type
I need to create a array of a generic type.
class CreateArray<ANY_TYPE> {
ANY_TYPE[] array = new ANY_TYPE[5];
ANY_TYPE[] createArray(int nLength) {
return (ANY_TYPE[])java.lang.refl ect.Array. newInstanc e(ANY_TYPE .class, nLength);
}
}
bout this codes don't compile.
Is this possible?
How?
class CreateArray<ANY_TYPE> {
ANY_TYPE[] array = new ANY_TYPE[5];
ANY_TYPE[] createArray(int nLength) {
return (ANY_TYPE[])java.lang.refl
}
}
bout this codes don't compile.
Is this possible?
How?
ASKER
where is the generic in there?
It doesn't need one. You can write it thus if you want:
class CreateArray<E, e> {
...
}
class CreateArray<E, e> {
...
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm speaking ok JAVA 1.5
My class has a generic and I need to return one array of that type.
Like in the example.
I know that I can do Object[]. But that was one of the JAVA 1.4 problems. Not checking types.
My class has a generic and I need to return one array of that type.
Like in the example.
I know that I can do Object[]. But that was one of the JAVA 1.4 problems. Not checking types.
class CreateArray {
private Class type;
public CreateArray(Class type) {
this.type = type;
}
public Object getArray() {
return java.lang.reflect.Array.ne
}
}