Scjp题目Question 174Click the Exhibit button.1.import java.util.*; 2.class KeyMaster { 3.public int i; 4.public KeyMaster(int i) { this.i = i; } 5.public boolean equals(Object o) { return i == ((KeyMaster)o).i; } 6.public int hashCode() { return i;

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/01 09:53:12

Scjp题目Question 174Click the Exhibit button.1.import java.util.*; 2.class KeyMaster { 3.public int i; 4.public KeyMaster(int i) { this.i = i; } 5.public boolean equals(Object o) { return i == ((KeyMaster)o).i; } 6.public int hashCode() { return i;
Scjp题目
Question 174
Click the Exhibit button.
1.import java.util.*;
2.class KeyMaster {
3.public int i;
4.public KeyMaster(int i) { this.i = i; }
5.public boolean equals(Object o) { return i == ((KeyMaster)o).i; }
6.public int hashCode() { return i; }
7.}
8.public class MapIt {
9.public static void main(String[] args) {
10.Set set = new HashSet();
11.KeyMaster k1 = new KeyMaster(1);
12.KeyMaster k2 = new KeyMaster(2);
13.set.add(k1); set.add(k1);
14.set.add(k2); set.add(k2);
15.System.out.print(set.size() + °:±
16.k2.i = 1;
17.System.out.print(set.size() + °:±
18.set.remove(k1);
19.System.out.print(set.size() + °:±
20.set.remove(k2);
21.System.out.print(set.size());
22.}
23.}
What is the result?
A.4:4:2:2
B.4:4:3:2
C.2:2:1:0
D.2:2:0:0
E.2:1:0:0
F.2:2:1:1
G.4:3:2:1
Answer:F
为什么不选c呢?

Scjp题目Question 174Click the Exhibit button.1.import java.util.*; 2.class KeyMaster { 3.public int i; 4.public KeyMaster(int i) { this.i = i; } 5.public boolean equals(Object o) { return i == ((KeyMaster)o).i; } 6.public int hashCode() { return i;
因为现在的k2和加入进去的k2不是同一个,所以你根本无法去掉原来那个.