Collection中的方法

以ArrayList为例

 1 package com.mydemo;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class CollectionDemo {
 6     public static void main(String[] args) {
 7     //        创建集合对象
 8                //Collection c = new Collection();//Collection是接口,不能实例化
 9         Collection c = new ArrayList();// 多态,父类引用子类对象
10               //boolean add(E e):添加元素
11         c.add("hello");    //因为ArrayList允许重复,所以永远可以添加成功
12          //void clear():清空集合
13              c.clear()
14              
15             //判断集合中是否包含指定元素:boolean contains(Object o)
16                System.out.println(c.contains("hello")); //如果集合中有hello,返回true
17         
18             //是否为空:boolean isEmpty()
19             System.out.println(c.isEmpty());
20     
21            //删除元素:boolean remove(Object o)
22            System.out.println(c.remove("hello"));
23 
24            //返回集合元素个数:int size()
25            System.out.println(c.size()); 
26 
27            //将集合转换成一个Object类型的数组:Object[] toArray()
28            Object[] objs = c.toArray();
29            for(int i = 0; i<objs.length; i++){
30                  System.out.println(objs[i]);
31             }
32 
33            
34         System.out.println(al);
35     }
36     
37 }        
原文地址:https://www.cnblogs.com/yifengs/p/10744918.html