如何将通用参数绑定到特定的List子类? [英] How to bind a generic parameter to a specific List subclass?
问题描述
public static< K,D extends List< T>,T>列表< Map< K,D>> meth(K k,D d){
List< Map< K,D>> daBytes =(List
编译器找出 如果您的意思是 I would expect in the code below : the compiler to figure out that and I need the cast anyway. If you mean for
这篇关于如何将通用参数绑定到特定的List子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! D
是 List< List<>>
和/或 T
是一个< List<>< / code> - 并且实际上摆脱了剧组。取而代之的是:
不能从ArrayList转换< Map< K,List< List< Byte>>>>到< Map< K,D>>< / code>< / p>
< p>是我想要以某种方式可能(没有策略模式解决方法)? C $ C>;你不能强迫它是
meth
中的其他特殊类型。如果按 D
您的意思是 List< List<>>
,那么您应该写:
List< Map< K,List< List< Byte>>>> daBytes = new ArrayList< Map< K,List< List< Byte>>>>();
D
是任意的,那么你应该写
列表< Map< K,D>> daBytes = new ArrayList< Map< K,D>>();
public static <K, D extends List<T>, T> List<Map<K, D>> meth(K k, D d) {
List<Map<K, D>> daBytes = (List<Map<K, D>>)
new ArrayList<Map<K, List<List<Byte>>>>();
// ...
}
D
is a List<List<Byte>>
and/or that T
is a <List<Byte>
- and to actually get rid of the cast. Instead I get :Cannot cast from ArrayList<Map<K,List<List<Byte>>>> to List<Map<K,D>>
Why ? Is what I want somehow possible (without the strategy pattern workaround) ?D
is specified by the caller of meth
; you can't force it to be some other particular type inside meth
. If by D
you mean List<List<Byte>>
, then you should write that: List<Map<K, List<List<Byte>>>> daBytes = new ArrayList<Map<K, List<List<Byte>>>>();
D
to be arbitrary, then you should be writingList<Map<K, D>> daBytes = new ArrayList<Map<K, D>>();