Redian新闻
>
如何造Array of Generic Type
avatar
如何造Array of Generic Type# Java - 爪哇娇娃
b*u
1
For example
public class SpecialArray {
T[] createSpecialArray()
{
return (T[])Array.newInstance(???, 10);
}
}
如何获得 T 的type at runtime by Reflection?
I am not allowed to change API or method argument).
So I can not use T[] createSpecialArray(Class clazz).
avatar
c*e
2
为什么不用arraylist ?

【在 b****u 的大作中提到】
: For example
: public class SpecialArray {
: T[] createSpecialArray()
: {
: return (T[])Array.newInstance(???, 10);
: }
: }
: 如何获得 T 的type at runtime by Reflection?
: I am not allowed to change API or method argument).
: So I can not use T[] createSpecialArray(Class clazz).

avatar
b*u
3
List is much better.
But I am not allowed to change API for compatibility reason.
avatar
g*g
4
If you can't change it, you can wrap it.

【在 b****u 的大作中提到】
: List is much better.
: But I am not allowed to change API for compatibility reason.

avatar
f*n
5
You can't. T doesn't exist at runtime. It's an illusion. Just remove T and
ask yourself how to do it. If you cannot do it without T, then you cannot do
it with T either:
public class SpecialArray {
Object[] createSpecialArray()
{
return (Object[])Array.newInstance(???, 10);
// How to get T?
// You do not pass anything into this method
}
}
Your problem is that arrays in Java know their component type at runtime. If
you do new String[5], the object knows it's an array of String at runtime.
However, generics does not exist at runtime. If you do new ArrayList
, it's just ArrayList at runtime. It does not know anything about String.
To create an array with a certain runtime type, you must know the component
type at runtime. Otherwise, the only thing your method can return is null:
T[] createSpecialArray()
{
return null;
}
What are you trying to do? It doesn't make sense for someone to ask you to
write a method like this.
If you are trying to create an array to use internally in your class, i.e.
public class SpecialArray {
private T[] elements = ???
}
then you have two options:
1. Create a new Object[], and make your internal variable type Object[].
That way, it is safe, but you need to cast to type T every time you access
an element.
public class SpecialArray {
private Object[] elements = new Object[5];
public T get(int i) { return (T)elements[i]; }
}
2. Create a new Object[], and then cast to T[], and have your internal
variable be type T[]. This way, you don't need to cast when you get elements
out. However, it is not safe, because the object's actual runtime type is
still Object[]. You need to make sure never to expose this internal variable
to the outside as type T[].
public class SpecialArray {
private T[] elements = (T[])new Object[5];
public T get(int i) { return elements[i]; }
// Do not do the following, will crash:
public T[] getElements() { return elements; }
}

【在 b****u 的大作中提到】
: For example
: public class SpecialArray {
: T[] createSpecialArray()
: {
: return (T[])Array.newInstance(???, 10);
: }
: }
: 如何获得 T 的type at runtime by Reflection?
: I am not allowed to change API or method argument).
: So I can not use T[] createSpecialArray(Class clazz).

avatar
s*e
6
(Class)((ParameterizedType)getClass() .
getGenericSuperclass()).getActualTypeArguments()[0];
but there is some limit on this approach. check with hibernate generic dao
implementation
avatar
W*o
7
我记得java不支持generic array啊

【在 b****u 的大作中提到】
: For example
: public class SpecialArray {
: T[] createSpecialArray()
: {
: return (T[])Array.newInstance(???, 10);
: }
: }
: 如何获得 T 的type at runtime by Reflection?
: I am not allowed to change API or method argument).
: So I can not use T[] createSpecialArray(Class clazz).

avatar
c*w
8
You can do it using option 1, option 2 is cleaner but requires changing of
your method api:
http://docs.oracle.com/javase/tutorial/extra/generics/literals.

【在 b****u 的大作中提到】
: For example
: public class SpecialArray {
: T[] createSpecialArray()
: {
: return (T[])Array.newInstance(???, 10);
: }
: }
: 如何获得 T 的type at runtime by Reflection?
: I am not allowed to change API or method argument).
: So I can not use T[] createSpecialArray(Class clazz).

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。