A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 邬jerry 中级黑马   /  2015-10-10 00:02  /  443 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Collection的功能概述:
* 1:添加功能
*                 boolean add(Object obj):添加一个元素
*                 boolean addAll(Collection c):添加一个集合的元素
* 2:删除功能
*                 void clear():移除所有元素
*                 boolean remove(Object o):移除一个元素
*                 boolean removeAll(Collection c):移除一个集合的元素(是一个还是所有)
* 3:判断功能
*                 boolean contains(Object o):判断集合中是否包含指定的元素
*                 boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(是一个还是所有)
*                                                                                                 只有包含所有的元素,才叫包含
*                 boolean isEmpty():判断集合是否为空
* 4:获取功能
*                 Iterator<E> iterator()(重点)
* 5:长度功能
*                 int size():元素的个数
*                 面试题:数组有没有length()方法呢?字符串有没有length()方法呢?集合有没有length()方法呢?
*                                 没有                        有                        没有
* 6:交集功能
*                 boolean retainAll(Collection c):两个集合都有的元素?思考元素去哪了,返回的boolean又是什么意思呢?
*                                 假设有两个集合A,B。
*                                A对B做交集,最终的结果保存在A中,B不变。
*                                返回值表示的是A是否发生过改变。
* 7:把集合转换为数组
*                 Object[] toArray()
*/
public class CollectionDemo {
        public static void main(String[] args) {
                Collection c = new ArrayList();

                c.add("hello");
                c.add("world");

                //boolean remove(Object o):移除一个元素
                c.remove("hello");
               
                //boolean contains(Object o):判断集合中是否包含指定的元素
                System.out.println(c.contains("hello"));
               
                //boolean isEmpty():判断集合是否为空
                System.out.println(c.isEmpty());
               
                //int size():元素的个数
                int x = c.size();
                System.out.println(x);

                System.out.println(c);
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马