Java 添加到未知类型的泛型列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6515228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java adding to a unknown type generic list
提问by ars265
I've come into something I haven't come across before in Java and that is, I need to create a new instance of say the ArrayList class at runtime without assigning a known type then add data to the list. It sounds a bit vague so here is an example:
我遇到了一些我以前在 Java 中没有遇到过的问题,也就是说,我需要在运行时创建一个新实例,比如说 ArrayList 类,而不分配已知类型,然后将数据添加到列表中。听起来有点含糊,所以这里是一个例子:
Class<?> c = i.getClass();
Constructor<?> con = ArrayList.class.getConstructor();
ArrayList<?> al = (ArrayList<?>)con.newInstance();
al.add("something");
Now the reason I'm doing this versus just using generics is because generics are already being used heavily and the "i" variable in this example would be given to use as type "?". I would really rather not throw in another generic as this would cause more work for the user and would be much less flexible in the end design. Is there any way to use something like below (Note: what is below doesn't work). Anyone have ideas?
现在我这样做而不是仅仅使用泛型的原因是因为泛型已经被大量使用,并且这个例子中的“i”变量将被用作类型“?”。我真的宁愿不加入另一个泛型,因为这会给用户带来更多的工作,并且最终设计的灵活性会大大降低。有什么方法可以使用下面的内容(注意:下面的内容不起作用)。有人有想法吗?
ArrayList<c> al = (ArrayList<c>)con.newInstance();
采纳答案by Snicolas
You can't add objects in a Collection defined using wildcards generics. This threadmight help you.
您不能在使用通配符泛型定义的集合中添加对象。这个线程可能会帮助你。
Indeed you are creating a collection that is, yes, the super type of every collection, and as such, can be assigned to any collection of generics; but it's too generic to allow any kind of add operation as there is no way the compiler can check the type of what you're adding. And that's exactly what generics are meant to : type checking.
实际上,您正在创建一个集合,是的,它是每个集合的超类型,因此可以分配给任何泛型集合;但是它太通用了,不允许任何类型的添加操作,因为编译器无法检查您添加的内容的类型。而这正是泛型的意义:类型检查。
I suggest you read the thread and see that it also apply to what you wanna do.
我建议你阅读这个线程,看看它也适用于你想做的事情。
Your collection is just too generic to allow anything to be added in. The problem has nothing to do with the right hand side of the asignment (using a singleton or reflection), it's in the left hand side declaration type using wildcards.
您的集合过于通用,无法添加任何内容。问题与分配的右侧无关(使用单例或反射),它位于使用通配符的左侧声明类型中。
回答by irreputable
If I get what you mean, you have a class C, which is unknown at compile time, and you want to create an ArrayList<C>
, in a type safe way. This is possible:
如果我明白你的意思,你有一个 C 类,它在编译时是未知的,你想以ArrayList<C>
类型安全的方式创建一个, 。这个有可能:
Class<?> c = ...;
ArrayList<?> al = listOf(c);
static <T> ArrayList<T> listOf(Class<T> clazz)
{
return new ArrayList<T>();
}
This is the theoreticallycorrect way of doing it. But who cares. We all know about type erasure, and there's no chance Java will drop type erasure and add runtime type for type parameters. So you can just use raw types and cast freely, as long as you know what you are doing.
这是理论上正确的做法。但谁在乎。我们都知道类型擦除,Java 不可能放弃类型擦除并为类型参数添加运行时类型。所以你可以只使用原始类型并自由转换,只要你知道你在做什么。
回答by Bohemian
You could just use ArrayList<Object>
, to which you can add()
anything.
你可以只使用ArrayList<Object>
, 你可以使用add()
任何东西。