scjp 里边的一道题,求解释。答案是D,我怎么感觉E对啊。
11. public void addStrings(List list) {12. list.add(”foo”);
13. list.add(”bar”);
14. }
What must you change in this method to compile without warnings?
A. add this code after line 11:
list = (List<String>) list;
B. change lines 12 and 13 to:
list.add<String>(”foo”);
list.add<String>(”bar”);
C. change the method signature on line 11 to:
public void addStrings(List<? extends String> list) {
D. change the method signature on line 11 to:
public void addStrings(List<? super String> list) {
E. No changes are necessary. This method compiles without warnings. --------------------编程问答-------------------- 会有警告的,你把代码放入Eclipse就知道了。
会警告你参数: List list 没有定义泛型。 --------------------编程问答-------------------- 怎么会是D呢 --------------------编程问答-------------------- 那像这样的,List list= new ArrayList();也没定义参数,怎么也行啊? --------------------编程问答--------------------
语法并没错误,但是跟你顶楼的问题一样,有警告啊:
Multiple markers at this line
- The value of the local variable list is not used
- List is a raw type. References to generic type List<E> should be parameterized
- ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized --------------------编程问答-------------------- What must you change in this method to compile without warnings?
对于支持泛型语法的编译器,如果你该使用泛型而没有使用泛型,编译时会有warning.在IDE里也可以看到 --------------------编程问答-------------------- 出现警告,但是还是能运行 --------------------编程问答-------------------- List<? super String> list
请问一下,这句是什么意思呀? --------------------编程问答-------------------- 在Myeclipse 中试一下就知道了,集合中用泛型是为了安全
补充:Java , Java SE