java 如何从 List<Object> 转换为 List<A> 或 List<?扩展 A>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14211999/
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
How to cast from List<Object> to List<A> or List<? extends A>?
提问by reverse_engineer
Possible Duplicate:
How do you cast a List of objects from one type to another in Java?
Searched the internet a little, and found no nice way of doing it... My solution now is:
在互联网上搜索了一下,并没有找到好的方法......我现在的解决方案是:
public class A {}
List<Object> obj = new ArrayList<Object>();
obj.add(new A());
// Ugly solution here:
List<A> a = (List<A>) (List) obj;
But this is quite ugly and gets a warning. No "official" way of doing this?
但这很丑陋,并会收到警告。没有这样做的“官方”方式?
EDIT: To the guys who closed this: I was aware of the solution posted in How do you cast a List of objects from one type to another in Java?It is the same as the one I posted in my question (just adding the <?>
after the first cast does exactly the same) I Was looking for something more "clean". In the direction of using the Class<?> clazz = listobj.get(0).getClass
way of getting the class and casting to the correct class at runtime (but no idea if something like that works... Eclipse doesn't seem to like it anyway...)
编辑:致关闭此问题的人:我知道如何在 Java 中将对象列表从一种类型转换为另一种类型中发布的解决方案?它与我在我的问题中发布的相同(只是<?>
在第一个演员之后添加完全相同)我正在寻找更“干净”的东西。在使用Class<?> clazz = listobj.get(0).getClass
获取类的方式并在运行时转换为正确的类的方向上(但不知道这样的东西是否有效......反正 Eclipse 似乎并不喜欢它......)
回答by Subhrajyoti Majumder
List<Object> obj = new ArrayList<Object>();
obj.add(new A());
It is not the right way to write code. Basically you are creating a generic List and adding Object to it and it type unsafe and keep any Object type.
这不是编写代码的正确方法。基本上,您正在创建一个通用列表并向其中添加 Object,它的类型不安全并保留任何 Object 类型。
List<Object> obj = new ArrayList<Object>();
obj.add(new A());
obj.add(new String("str"));
obj.add(1);
It is recommended to create type-safe List
like List<A> obj = new ArrayList<A>();
建议创建类型安全的List
样List<A> obj = new ArrayList<A>();
you can do this in such a way -
你可以这样做 -
public <T>List<T> castCollection(List srcList, Class<T> clas){
List<T> list =new ArrayList<T>();
for (Object obj : srcList) {
if(obj!=null && clas.isAssignableFrom(obj.getClass()))
list.add(clas.cast(obj));
}
return list;
}
回答by Brian Agnew
So why isn't your list declared as
那么为什么你的列表没有被声明为
List<A> obj = new ArrayList<A>();
to begin with ?
开始?
It's really dangerous to do what you're trying to do. What you're saying is that your list contains A
s and subclasses thereof. But since it's originally a list of Object
s (i.e. anything). You're likely to get a nasty surprise later on.
做你想做的事真的很危险。您的意思是您的列表包含A
s 及其子类。但是因为它最初是一个Object
s列表(即任何东西)。稍后你可能会得到一个令人讨厌的惊喜。
What that compiler error really means is that somewhere you have a design issue.
编译器错误的真正含义是您在某个地方遇到了设计问题。
回答by Anders R. Bystrup
Perhaps the issue is that you want to put objects of different (but inheritance-related) types into the list? In that case, you read the Generics Tutorial, especially the parts about <? extends Something>
and <? super Something>
也许问题是您想将不同(但与继承相关)类型的对象放入列表中?在那种情况下,你阅读泛型教程,特别是关于<? extends Something>
和的部分<? super Something>
Cheers,
干杯,