为了账号安全,请及时绑定邮箱和手机立即绑定

请教两个JAVA问题

请教两个JAVA问题

翻翻过去那场雪 2019-03-14 14:15:56
第一题:(答案:F)Given the following method signatures from ArrayList:boolean add(E e)protected void removeRange(int fromIndexInclusive, int toIndexExclusive)int size()and given:  2. import java.util.*; 3. public class MyUtil extends ArrayList { 4. public static void main(String[] args) { 5. MyUtil m = new MyUtil(); 6. m.add("w"); m.add("x"); m.add("y"); m.add("z"); 7. m.removeRange(1,3); 8. System.out.print(m.size() + " "); 9. MyUtil m2 = new MyUtil2().go(); 10. System.out.println(m2.size()); 11. } 12. } 13. class MyUtil2 { 14. MyUtil go() { 15. MyUtil m2 = new MyUtil(); 16. m2.add("1"); m2.add("2"); m2.add("3"); 17. m2.removeRange(1,2); 18. return m2; 19. } } What is the result?A. 1 1B. 1 2C. 2 1D. 2 2E. An exception is thrown at runtime.F. Compilation fails due to a single error.G. Compilation fails due to multiple errors. 第二题:(答案:F)Given: 1. public class Hose <E extends Hose> { 2. E innerE; 3. public static E doStuff(E e, Hose<E> e2) { 4. // insert code here 5. } 6. public E getE() { 7. return innerE; 8. } } Which can be inserted, independently at line 4, for the code to compile? (Choose all that apply.)A. return e;B. return e.getE();C. return e2;D. return e2.getE();E. return new Hose().getE();F. Compilation fails regardless of which return is inserted.  请大家帮忙解释下原因,多谢啦!
查看完整描述

2 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

removeRange是protected方法,在ArrayList及其子类以外是不可见的。

E是动态类型的范型,静态是不能用的。


查看完整回答
反对 回复 2019-04-26
?
长风秋雁

TA贡献1757条经验 获得超7个赞

第一题:
B
第二题:
F

原因如下:
第一题:removeRange(int fromIndex, int toIndex) 
移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素。
第二题:
public static E doStuff(E e, Hose e2) {

其实错误出在这一句,这是一个静态方法,而且是泛型的,静态的泛型方法中,E需要先定义才能使用,这里的E并不能使用到public class Hose { 这个地方的E。
所以,无论你在方法中填写什么内容,都会报编译错误;
可以将static移除掉,然后就可以选择:
A、B、D、E了。


查看完整回答
反对 回复 2019-04-26
  • 2 回答
  • 0 关注
  • 607 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号