Collection集合的方法(笔记)

笔记

 1 package Collection;
 2 import java.util.ArrayList;
 3 import java.util.Collection;
 4 public class Collerction_demo01 {
 5 
 6 /**
 7 *    集合Collection接口的方法
 8 *    add()  clear()  contains()
 9 *    JAVA中三种长度的表现形式
10 *    1.数组.length属性 返回 int
11 *    2.字符串.length()方法,返回int
12 *    3.集合.size()方法,返回int
13 *
14 */
15 public static void main(String[]arg){
16     //function01();
17     function02();
18 }
19 public static void function01(){
20     //创建一个ArrayList集合,指定泛型String
21     Collection<String> coll = new ArrayList<String>();
22     //Collection的add()和clear()方法
23     coll.add("abc");//添加字符串abc
24     coll.add("def");//添加字符串def
25     System.out.println(coll);//打印coll,输出[abc,def]
26     
27     coll.clear();//集合清空
28     System.out.println(coll);//输出[]
29     coll.add("a");
30     coll.add("b");
31     coll.add("c");
32     System.out.println(coll.contains("a"));//输出 true
33     //contains(Object o)    判断o是否存在于coll集合中    
34     }
35 /**
36  * 集合转数组方法toArray()
37  * 移除集合的一个元素 remove()
38  */
39 
40 public static void function02() {
41     Collection<String> coll = new ArrayList<String>();
42     coll.add("hello");
43     coll.add("word");
44     coll.add("are");
45     coll.add("you");
46     coll.add("ok?");
47     Object[] objs = coll.toArray();//集合转成数组
48     for(Object obj:objs) {
49         System.out.print(obj+"\t");//输出    hello  word    are       you     ok?
50     }
51     coll.remove("you");//删除 you 这个元素
52     System.out.println(coll);//输出 [hello, word, are, ok?]
53     
54 }
55 
56 
57 }

 



posted @ 2017-11-23 15:04  默默向风呆  阅读(378)  评论(0编辑  收藏  举报