你的代码没问题,分析都正确,不过有些没必要的语句不必写上,给个模版你。- package home.test;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.TreeSet;
- public class CollectionTest {
- public static void main(String[] args) {
- Set<Integer> keys = new HashSet<Integer>();
- Set<Integer> doors = new TreeSet<Integer>();
- for (int i = 0; i <100; i++) {
- keys.add(i);
- doors.add(i);
- }
- System.out.println(keys);
- System.out.println(doors);
-
- int count = 0;
- for (Integer door : doors) {
- for (Integer key : keys) {
- System.out.println(door+"=="+key+"? "+(door==key));
- count++;
- if (key == door){
- keys.remove(key);
- break;
- }
- }
- }
- System.out.println(count);
- }
- }
复制代码 |