因爲我們知道我們可以在列表中添加字符串和整數,如果我們聲明列表的通用類型。但代碼似乎並沒有加上字符串。有什麼問題我不明白..爲什麼不能在列表中添加字符串?
class GHJ{String s;GHJ(String s){this.s=s;}}
class DFR{
public static void main(String[] g){
List<? extends Object> list=new ArrayList();
list.add((new String("ffg")));// compiler error
list.add("df");// compiler error
list.add("sdd");// compiler error
list.add(new GHJ("dff"));// compiler error
String s= list.get(0);// compiler error
System.out.println(list+" "+s);
}
}
你是什麼意思'不添加字符串'你期望這個代碼做什麼? – 2010-01-29 08:37:08
我們曾經這樣做:) – 2010-01-29 11:04:21