我有两个班。其中一个类(Class_1)的有一个返回列表的方法:为什么我在这里没有安全或不安全的操作?
class Class_1{
....
public List<String> method_1(){
Statement st = null;
ResultSet res = null;
List<String> tables = new ArrayList<String>();
try {
st = this.cnx.createStatement();
res = st.executeQuery("SHOW TABLES");
while(res.next()){
tables.add(res.getString(1));
}
}
catch(Exception e){
e.printStackTrace();
}
return tables;
}
}
当我编译这个(Class_1)班,我没有得到任何错误或警告。但我有另一班Class_2,它使用这个第一类。在这第二类我的代码,这样行:
class Class_2{
public static void main(String args[]){
Class_1 c = new Class_1();
List<String> some_list = c.method_1();
}
}
当我做以上(Class_2)之类的编辑,我现在得到这些警告,说我不喜欢压抑:
注:Class_2.java使用未经检查或不安全的操作
注:与-Xlint重新编译:未选中的细节
我不知道为什么我得到这些电子因为在我看来,一切都应该没问题 - 一种方法显然只返回List<String>
,另一段代码试图将此列表分配给类型为List<String>
的变量。我看不到,这里有什么不安全的。
编辑
当我编译我的Class_1与-XLint:unchecked
我得到:
列表some_list = c.method_1();
^
要求:列表[字符串]
发现:名单
这是为什么?
编辑
我做了一些调查,发现,整个问题是在这一行:
tables.add(res.getString(1));
这似乎是一个字符串,但是编译器不喜欢这一点。所以,如果我简单地切换到:
tables.add("Some real string");
然后我得不到任何警告。真奇怪。
我觉得有些东西你没有告诉我们。你能包括更多细节吗? –
这看起来像你可能已经修剪你的代码太多了。难道说Class_1实际上是一个类的参数吗?如果你不提供这些,你也会得到这些错误 – Vogel612
不,这些不是gerenric类。这是两个非常简单的类。 – Jacobian