集合(练习2)

 1 package bao4;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashSet;
 5 
 6 public class Jihe2 
 7 {
 8     public static void main(String[] args)
 9     {
10         ArrayList<String> ls=new ArrayList<String>();
11         ls.add("A");
12         ls.add("a");
13         ls.add("C");
14         ls.add("c");
15         if(ls.add("a"))
16         {
17             System.out.println("数据已插入");
18         }
19         else
20         {
21             System.out.println("数据插入失败");
22         }
23         for(String i:ls)
24         {
25             System.out.println(i);
26         }
27         
28         
29         
30         HashSet<String> hh=new HashSet<String>();
31         hh.add("A");
32         hh.add("a");
33         hh.add("C");
34         hh.add("c");
35         if(hh.add("a"))
36         {
37             System.out.println("数据已插入");
38         }
39         else
40         {
41             System.out.println("数据插入失败");
42         }
43         for(String i:hh)
44         {
45             System.out.println(i);
46         }
47         
48     }
49 //分别向Set集合以及List集合中添加"A","a","C","c","a",5个元素,观察重复值"a"能否在Set和List集合中添加成功
50 }

posted @ 2016-05-29 22:13  明天会更好!!!!  阅读(132)  评论(0)    收藏  举报