&QUOT;不能创建通用阵列..&QUOT; - 如何创建地图&LT的数组;字符串对象&gt ;? [英] "Cannot create generic array of .." - how to create an Array of Map<String, Object>?
问题描述
我想用SimpleJdbcInsert类类executeBatch方法
I would like to use simpleJdbcInsert class and executeBatch method
public int[] executeBatch(Map<String,Object>[] batch)
<一个href=\"http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html\">http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html
所以,我需要通过的数组地图&LT;弦乐,对象&gt;
作为参数。如何创建这样的阵列?
我试过是
So I need to pass an array of Map<String,Object>
as parameter. How to create such an array?
What I tried is
Map<String, Object>[] myArray = new HashMap<String, Object>[10]
有错误:无法创建地图&LT通用阵列;弦乐,对象&gt;
A 列表与LT;地图&LT;弦乐,对象&gt;&GT;
会更容易些,但我想我需要一个数组。那么如何创建的数组地图&LT;弦乐,对象&gt;
?
谢谢
A List<Map<String, Object>>
would be easier, but I guess I need an array. So how to create an array of Map<String, Object>
?
Thanks
推荐答案
[] )。相反,你创建原始类型的数组(地图[]
),并将其转换为地图&LT;弦乐,对象&gt; []
。这将导致不可避免的(但SUP pressible)编译器警告。
Because of how generics in Java work, you cannot directly create an array of a generic type (such as Map<String, Object>[]
). Instead, you create an array of the raw type (Map[]
) and cast it to Map<String, Object>[]
. This will cause an unavoidable (but suppressible) compiler warning.
这应该为你所需要的工作:
This should work for what you need:
Map<String, Object>[] myArray = (Map<String, Object>[]) new Map[10];
您可能希望从正在显示注释这与发生在@苏pressWarnings方法(未登记)
,以prevent警告。
You may want to annotate the method this occurs in with @SupressWarnings("unchecked")
, to prevent the warning from being shown.
这篇关于&QUOT;不能创建通用阵列..&QUOT; - 如何创建地图&LT的数组;字符串对象&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!