关于集合的练习P235-1,2,3

第一题:

  

 1 import java.util.*;
 2 
 3 public class ListTest {
 4 
 5     
 6     public static void main(String[] args) {
 7         
 8         ArrayList<Integer> list = new ArrayList<Integer>();
 9         
10         for(int i=0;i<101;i++){
11             
12             list.add(i);
13         }
14         System.out.println("原先的数据:\n"+list);
15         
16         list.remove(10);
17         
18         System.out.println("移除之后的数据:\n"+list);
19     }
20 }

运行的结果:

原先的数据:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
移除之后的数据:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

第二题:

  

 1 import java.util.*;
 2 
 3 public class SetList {
 4 
 5     public static void main(String[] args) {
 6         
 7         ArrayList<String> list = new ArrayList<>();
 8         HashSet<String> set = new HashSet<String>();
 9         TreeSet<String> set1 = new TreeSet<String>();
10         
11         Collections.addAll(list, "A","a","c","C","a");
12         Collections.addAll(set, "A","a","c","C","a");
13         Collections.addAll(set1, "A","a","c","C","a");
14         
15         System.out.println("list的数据:\t"+list);
16         System.out.println("set的数据:\t"+set);
17         System.out.println("set的数据:\t"+set1);
18         
19     }
20 }

运行的结果:

list的数据: [A, a, c, C, a]
set的数据: [c, A, C, a]
set的数据: [A, C, a, c]

第三题:

 1 import java.util.*;
 2 
 3 public class TestMap {
 4 
 5     public static void main(String[] args) {
 6         
 7         Emp e1 = new Emp("001","小赵");
 8         Emp e2 = new Emp("002","小钱");
 9         Emp e3 = new Emp("003","小孙");
10         Emp e4 = new Emp("004","小李");
11         Emp e5 = new Emp("005","小周");
12         Emp e6 = new Emp("006","小吴");
13         
14         HashMap<String,String> m = new HashMap<String,String>();
15         
16         m.put(e1.getId(), e1.getName());
17         m.put(e2.getId(), e2.getName());
18         m.put(e3.getId(), e3.getName());
19         m.put(e4.getId(), e4.getName());
20         m.put(e5.getId(), e5.getName());
21         m.put(e6.getId(), e6.getName());
22         
23         System.out.println("原始的数据:\n"+m);
24         
25         m.remove("005");
26         
27         System.out.println("移除之后的数据:\n"+m);
28         
29         
30     }
31 }
32 class Emp{
33     
34     private String id;
35     private String name;
36     public Emp(String id, String name) {
37         super();
38         this.id = id;
39         this.name = name;
40     }
41     public String getId() {
42         return id;
43     }
44     public void setId(String id) {
45         this.id = id;
46     }
47     public String getName() {
48         return name;
49     }
50     public void setName(String name) {
51         this.name = name;
52     }
53     
54 }

运行的结果:

原始的数据:
{004=小李, 005=小周, 006=小吴, 001=小赵, 002=小钱, 003=小孙}
移除之后的数据:
{004=小李, 006=小吴, 001=小赵, 002=小钱, 003=小孙}

 

你可能感兴趣的:(关于集合的练习P235-1,2,3)