类型安全性:ArrayList []类型的表达式需要未检查的转换 [英] Type safety: The expression of type ArrayList[] needs unchecked conversion
问题描述
我正在一个项目中,我得到TypeSafety问题在初始化一个特定大小的方法。在我的run方法中,我在 new ArrayList [tableLists.size()]
上有黄线并抱怨 -
I am working on a project in which I am getting TypeSafety issues on initializing one method with a particular size. In my run method, I have yellow line on the new ArrayList[tableLists.size()]
and complaining about-
类型安全性:类型ArrayList []的表达式需要未检查的转换以符合ArrayList< Method> []
下面是代码。
private ArrayList<Method> methods[] = null;
@Override
public void run() {
methods = new ArrayList[tableLists.size()];
}
如何解决此问题 TypeSafety
在这里?
How can I fix this TypeSafety
issue here?
更新: - b
int j = 0;
dbConnection = new Connection[tableLists.size()];
callableStatement = new CallableStatement[tableLists.size()];
methods = new ArrayList[tableLists.size()];
//loop around the map values and make the connection list
for (Map<String, String> map : tableLists.values()) {
dbConnection[j] = getDBConnection(map.get("URL"), map.get("USER"), map.get("PASSWORD"), map.get("DRIVER"));
callableStatement[j] = dbConnection[j].prepareCall(map.get("SQL"));
methods[j] = getRequiredMethods(map.get("SUFFIX"));
j++;
}
推荐答案
t似乎以任何特定的方式分离,为什么不将它们全部存储在相同的 ArrayList
中,完全绕过问题?
Since the methods themselves don't seem to be separated in any particular way, why not just store them all in the same ArrayList
, bypassing the problem entirely?
ArrayList<Method> methods;
/* ... */
methods.addAll(getRequiredMethods(map.get("SUFFIX")));
数组和泛型往往不会彼此发挥非常好。如果您真的需要分隔,您必须选择主要选项:
Arrays and generics tend not to play very nice with each other. If you really need the separation, you have to primary options:
-
使用
ArrayList< ArrayList< Method>>
而不是数组。这将让你处理一切很好,而不使用平面数组。然后,您可以有效地初始化方法
对象的大小就像一个常规的数组。
Use an
ArrayList<ArrayList<Method>>
instead of an array. This will let you handle everything nicely without mucking with plain arrays. You can then efficiently initialize themethods
object with a size just like with a regular array.
methods = new ArrayList<ArrayList<Method>>(tableLists.size());
@SuppressWarnings(unchecked)
注释来抑制警告。这是丑陋和恼人,所以我会避免它,如果你可以。
@SuppressWarnings("unchecked")
annotation. This is ugly and annoying, so I'd avoid it if you can.这篇关于类型安全性:ArrayList []类型的表达式需要未检查的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!