由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 如何造Array of Generic Type
相关主题
请问有没有generic的arrayextending generic class , but not mentioning its parameterized type?
Java练习题 12问个 Generic 的问题
问一个Java的问题,关于create generic arrayjava这个是什么逻辑?
Java里有没有象cell array一样的东西ArrayList and Link list
ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有Java runtime array memory layout?
error: generic array creationHow to check if an element is in an array?
Java里面有没有可能写个带generic parameter的class对built-in type也适用?Array 能不能用ArrayList作为它的元素?
我想把一个Arraylist转成String[]如何定义这样的数组?
相关话题的讨论汇总
话题: object话题: type话题: runtime话题: class
进入Java版参与讨论
1 (共1页)
b****u
发帖数: 1130
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).
c*********e
发帖数: 16335
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).

b****u
发帖数: 1130
3
List is much better.
But I am not allowed to change API for compatibility reason.
g*****g
发帖数: 34805
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.

f*******n
发帖数: 12623
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).

s******e
发帖数: 493
6
(Class)((ParameterizedType)getClass() .
getGenericSuperclass()).getActualTypeArguments()[0];
but there is some limit on this approach. check with hibernate generic dao
implementation
W***o
发帖数: 6519
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).

c*********w
发帖数: 65
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).

1 (共1页)
进入Java版参与讨论
相关主题
如何定义这样的数组?ArrayList vs Array, StringBuffer vs String, 大侠们给讲讲有
关于得到generic type的问题求助!error: generic array creation
今天碰到一个面试题Java里面有没有可能写个带generic parameter的class对built-in type也适用?
immutable list我想把一个Arraylist转成String[]
请问有没有generic的arrayextending generic class , but not mentioning its parameterized type?
Java练习题 12问个 Generic 的问题
问一个Java的问题,关于create generic arrayjava这个是什么逻辑?
Java里有没有象cell array一样的东西ArrayList and Link list
相关话题的讨论汇总
话题: object话题: type话题: runtime话题: class