本文介绍了“无法创建 .. 的通用数组"- 如何创建 Map<String, Object> 的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 simpleJdbcInsert 类和 executeBatch 方法

I would like to use simpleJdbcInsert class and executeBatch method

public int[] executeBatch(Map<String,Object>[] batch)

http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/jdbc/core/simple/SimpleJdbcInsert.html

所以我需要传递一个 Map 数组作为参数.如何创建这样的数组?我尝试的是

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]

错误:无法创建 Map

A List<Map<String, Object>> 会更容易,但我想我需要一个数组.那么如何创建 Map<String, Object> 的数组呢?谢谢

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

推荐答案

由于Java中泛型的工作方式,不能直接创建泛型类型的数组(如Map[]).相反,您创建一个原始类型 (Map[]) 的数组并将其转换为 Map[].这将导致不可避免(但可抑制)的编译器警告.

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];

您可能希望使用 @SuppressWarnings("unchecked") 注释发生这种情况的方法,以防止显示警告.

You may want to annotate the method this occurs in with @SuppressWarnings("unchecked"), to prevent the warning from being shown.

这篇关于“无法创建 .. 的通用数组"- 如何创建 Map&lt;String, Object&gt; 的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:55