嗨,我有一个情况将对象转换为列表.
我从请求参数中获取了对象值List.
我需要将此Object转换为List
这个类名我可以在运行时获得String值.
样品:
private static Object createList(Object object){
private static Object createList(Object object){
List dd = (List)object;
//the List contains the objects of this class
Class cls = Class.forName(dd.get(0).getClass().getName());
//I would like to cast this Object to List<thisClass>
}
Run Code Online (Sandbox Code Playgroud)
}
这怎么可以实现?有人可以帮助我吗?
基本上,我不是真的推荐,你可以做这样的事情,甚至不需要类型限制:
public static void main(String[] args) {
Object list = new ArrayList<String>();
List<Integer> integers = cast(list);
}
@SuppressWarnings("unchecked")
public static <T extends List<?>> T cast(Object obj) {
return (T) obj;
}
Run Code Online (Sandbox Code Playgroud)
它还显示了它引入的问题
| 归档时间: |
|
| 查看次数: |
63761 次 |
| 最近记录: |