SCJP題庫第195題


Given:
3. import java.util.*;
4. public class Mapit{
5.   public static void main(String[] args){
6.     Set<Integer> set = new HashSet<Integer>();
7.     Integer i1 = 45;
8.     Integer i2 = 46;
9.     set.add(i1);
10.     set.add(i1);
11.     set.add(i2); System.out.print(set.size() + " ");
12.     set.remove(i1); System.out.print(set.size() + " ");
13.     i2 = 47;
14.     set.remove(i2); System.out.print(set.size() + " ");
15.   }
16.}
What is the result?
 
 
A. 2 1 0
B. 2 1 1
C. 3 2 1
D. 3 2 2
E. Compilation fails.
F. An exception is thrown at runtime. 
 
Ans: B
 
解說: 
HashSet湊雜集合,用物件的特徵值計算出雜湊值,此題用Integer物件計算雜湊值,只要Integer的數字一樣計算出的雜湊值就一樣,一樣的雜湊值物件不論加(add)幾次都不會重複加入雜湊集合中。移除物件也是依據雜湊值來找出對應的物件。

Comments

Popular posts from this blog

Android-使用webview在V3版的Google地圖GPS定位