论坛首页 入门讨论版 Java

谁能帮我看下这一题testkiller Question 11

浏览 168 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-04-28
Question 11
Given:
23. Object [] myObjects = {
24. new integer(12),
25. new String(”foo”),
26. new integer(5),
27. new Boolean(true)
28. };
29. Arrays.sort(myObjects);
30. for( int i=0; i<myObjects.length; i++) {
31. System.out.print(myObjects[i].toString());
32. System.out.print(” “);
33. }

What is the result?
A. Compilation fails due to an error in line 23.
B. Compilation fails due to an error in line 29.
C. A ClassCastException occurs in line 29.
D. A ClassCastException occurs in line 31.
E. The value of all four objects prints in natural order.

Answer: C

答案是C,我觉得应该是b,
   
最后更新时间:2008-04-29
看JDK 源码。

Arrays.sort :
...
    ((Comparable) dest[j-1]).compareTo(dest[j])>0
...

Iteger.compareTo:
...
public int compareTo(Integer anotherInteger)
...

抛出ClassCastException.
   
0 请登录后投票
最后更新时间:2008-04-30
懂了,但是为什么运行的时候抛出的是ArrayIndexOutOfBoundsException?
   
0 请登录后投票
论坛首页 入门讨论版 Java

跳转论坛:
JavaEye推荐