上期答案
問題1. 給出如下函數:
11. public static int sum(List list) {
12. int sum = 0;
13. for ( Iterator iter = list.iterator();iter.hasNext(); ) {
14. int i =((Integer)iter.next()).intValue();
15. sum += i;
16. }
17. return sum;
18. }
請在下列選項中選出三個改動,使得函數可以使用泛型且不會提示泛型未檢測警告?
(Choose three.)
A. Remove line 14.
B. Replace line 14 with "int i =iter.next();".
C. Replace line 13 with "for (int i : intList){".
D. Replace line 13 with "for (Iterator iter :intList) {".
E. Replace the method declaration with"sum(List<int> intList)".
F. Replace the method declaration with"sum(List<Integer> intList)".
答案 A C F
知識點:
1. 在JAVA的泛型中不能使用基本數據類型
2. 使用迭代器遍歷容器
遍歷容器的三種方式
List<String> userlist = newArrayList<String>();
userlist.add(“aa");
userlist.add(“bb");
userlist.add(“cc");
//使用普通for循環
for(int i=0; i<userlist.size(); i++){ }
//使用Iterator迭代器
Iterator it = userlist.iterator();
while(it.hasNext()){ }
for (;it.hasNext();it.next() ){ }
//使用增強for循環
for(String s : userlist){ }
問題2:在算法中要求使用java.util.List數據結構,該算法要求可以方便的添加“add”一個元素,但是不要求支持隨機快速訪問元素,請問你會選擇下列的那個類?
A. java.util.Queue
B. java.util.ArrayList
C. java.util.LinearList
D. java.util.LinkedList
答案:D
知識點:
接口List與Queue的異同
ArrayList與LinkedList的異同
Queue接口與List、Set同一級別,都是繼承了Collection接口。
java.util.Queue接口,用以支持隊列的常見操作。
Queue使用時要儘量避免Collection的add()和remove()方法,而是要使用offer()來加入元素,使用poll()來獲取並移出元素。它們的優 點是通過返回值可以判斷成功與否,add()和remove()方法在失敗的時候會拋出異常。如果要使用前端而不移出該元素,使用element()或者peek()方法
ArrayList與LinkedList都是List接口的實現類
ArrayList實現了List接口,它是以數組的方式來實現的,數組的特性是可以使用索引的方式來快速定位對象的位置,因此對於快速的隨機取得對象的需求,使用ArrayList實現執行效率上會比較好。
LinkedList是採用鏈表的方式來實現List接口的,採用鏈表實現的。
LinearList 雙向鏈表,JAVA中沒有這個類
問題3:
11. // 此處插入代碼
12. private N min max;
13. publicN getMin() { return min; }
14. publicN getMax() { return max; }
15. publicvoid add(N added) {
16. if (min == null )
17. min = added;
18. if (max == null )
19. max = added;
20. }
21. }
下列選項中,哪兩個插入第11行位置後可以是的代碼完整且正確:
A. public class MinMax<?> {
B. public class MinMax<? extends Number> {
C. public class MinMax<N extends Object> {
D. public class MinMax<N extends Number> {
E. public class MinMax<? extends Object> {
F. public class MinMax<N extends Integer> {
答案: D F
知識點:
在泛型中通配符“?”的用法
泛型的上下限概念
問題4:
12. import java.util.*;
13. public class Explorer2 {
14. publicstatic void main(String[] args) {
15. TreeSet<Integer>s = new TreeSet<Integer>();
16. TreeSet<Integer>subs = new TreeSet<Integer>();
17. for(int i = 606; i < 613; i++)
18. if(i%2== 0) s.add(i);
19. subs= (TreeSet)s.subSet(608, true, 611, true);
20. s.add(629);
21. System.out.println(s+ " " + subs);
22. }
23. }
以上代碼的運行結果是
A. 編譯失敗.
B. 運行時有異常拋出.
C. [608 610 612 629] [608 610]
D. [608 610 612 629] [608 610 629]
E. [606 608 610 612 629] [608 610]
F. [606 608 610 612 629] [608 610 629]
Answer: E
知識點:
請參考JAVA幫助文檔中關於Set接口部分的說明
public NavigableSet<E> subSet(EfromElement,
booleanfromInclusive,
EtoElement,
booleantoInclusive)
fromElement - 返回 set 的低端點
fromInclusive - 如果低端點要包含在返回的視圖中,則爲 true
toElement - 返回 set 的高端點
toInclusive - 如果高端點要包含在返回的視圖中,則爲 true
第五題
1. public class Score implements Comparable<Score>{
2. privateint wins losses;
3. publicScore(int w int l) { wins = w; losses = l; }
4. publicint getWins() { return wins; }
5. publicintgetLosses() { return losses; }
6. publicString toString() {
7. return"<" + wins + "" + losses + ">";
8. }
9. // insertcode here
10. }
下列那個方法可以使得該類完整?
A. public int compareTo(Object o){/*more codehere*/}
B. public int compareTo(Score other){/*more codehere*/}
C. public int compare(Score s1Score s2){/*more codehere*/}
D. public int compare(Object o1Object o2){/*morecode here*/}
知識點:
Comparable接口的實現
--------------------------------------------------分割線------------------------------------------------
本期新題
第六題
11. public class Person {
12. privateString name;
13. publicPerson(String name) {
14. this.name = name;
15. }
16. public int hashCode() {
17. return 420;
18. }
19. }
下列哪個敘述是正確的:
A.在HashMap查找某個Person鍵值所需要的時間依賴於Map的大小。
B.執行刪除一個Person鍵的操作,將刪除HashMap中所有類型爲Person的鍵
C.在HashSet中插入第二個Person對象,將導致第一個Person對象被移除。
D. 在HashSet中查找一個Person對象是否存在的時間是一個常數,不依賴於HashSet的大小
第七題
5. import java.util.*;
6. public class SortOf {
7. public static void main(String[] args) {
8. ArrayList<Integer> a = newArrayList<Integer>();
9. a.add(1); a.add(5); a.add(3);
11. Collections.sort(a);
12. a.add(2);
13. Collections.reverse(a);
14. System.out.println(a);
15. }
16. }
該程序片段的運行結果是?
A. [1 2 3 5]
B. [2 1 35]
C. [2 5 31]
D. [5 3 21]
E. [1 3 52]
F. 編譯失敗
G. 運行時有異常拋出
第八題
Given
11. public interface Status {
12. /* 此處插入代碼*/ int MY_VALUE = 10;
13. }
下列哪三個選項可以用於第12行
A. final
B. static
C. native
D. public
E. private
F. abstract
G. protected
第九題
5. class Atom {
6. Atom() { System.out.print("atom "); }
7. }
8. class Rock extends Atom {
9. Rock(String type) { System.out.print(type); }
10. }
11. public class Mountain extends Rock {
12. Mountain() {
13. super("granite ");
14. new Rock("granite ");
15. }
16. public static voidmain(String[] a){ newMountain(); }
17. }
上述代碼片段的運行結果是?
A. 編譯失敗.
B. atom granite
C. granite granite
D. atom granite granite
E. An exception is thrown at runtime.
F. atom graniteatom granite
第十題
10. class Line {
11. public class Point { public int x, y;}
12. public Point getPoint() { return new Point(); }
13. }
14. class Triangle {
15. public Triangle() {
16. // 此處插入代碼
17. }
18. }
在16行處插入代碼獲得一個Class Point的實例?
A. Point p = Line.getPoint();
B. Line.Point p = Line.getPoint();
C. Point p = (new Line()).getPoint();
D. Line.Point p = (newLine()).getPoint();