• java集合框架初探


    Collection接口中的共性功能
    1.添加
    boolean add(object obj);一次添加一个
    boolean addAll(Collection);将指定容器中的所有元素添加

    2.删除
    void clear();
    boolean removbe(object o);
    boolean removeAll(Collection c);
    3.获取长度
    int size()
    4.判断
    boolean isEmpty();
    boolean contains(object o)
    boolean contains(object)
    5.将集合转成数组
    toArray();
    toArray([]);

    5.取出集合元素iterator
    Iterator iterator()
    获取集合中元素上迭代功能的迭代器对象
    迭代:取出元素的一种方式
    迭代器:具备着迭代功能的对象
    而迭代器对象不需要new 直接通过 iterator()获取即可。

    迭代器是取出collection集合元素中的公共方法

    package com.runoob.test;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    public class CollectionDemo {
        public static void main(String[] args) {
            /*
             * 演示Collection中的基本功能
             * */
            Collection c1=new ArrayList();
            Collection c2=new ArrayList();
            /*
             * 添加单个元素(对象)
             */
            c1.add("abc1");
            c1.add("abc2");
            c1.add("abc3");
            
            c2.add("abc1");
            c2.add("abc2");
            c2.add("abc7");
            
    //        System.out.println("c1 contains "abc1"="+c1.contains("abc1"));//结果为true
    //        System.out.println("c1 contains "abc"="+c1.contains("abc"));//结果为false
    //        System.out.println(c1.remove("abc1"));//结果为true,代表删除成功
    //        System.out.println(c1.remove("abc"));//结果为false,代表删除失败
            
    //        c1.clear();//清空c1
    //        System.out.println(c1);//结果为[]
    //        System.out.println(c1.isEmpty());结果为true
            
    //        c1.remove("abc1");
    //        System.out.println(c1);//[abc2,abc3]
            
            int a=c1.size();
            System.out.println(a);
            
    //======带all的方法=======        
    //        c1.removeAll(c2);
    //        System.out.println(c1);//结果为[abc3],删除c1中和c2相同的部分
            
    //        c1.retainAll(c2);
    //        System.out.println(c1);//结果为[abc1,abc2],返回c1和c2的交集
            
    //    
    //        c1.addAll(c2);
    //        System.out.println(c1);//结果为[abc1, abc2, abc3, abc1, abc2, abc7]
    //        boolean b=c1.containsAll(c2);
    //        System.out.println(b);//结果为true
    
    
            
            
            
    
    
    
            
    
        }
    
    
    }
  • 相关阅读:
    OEA框架学习:缓存
    2012年 博文整理
    技术支持经验总结
    OEA框架学习:元数据设计
    安装后新建Android出现“AndroidManifest.xml 系统找不到指定的文件”解决方案
    Android控件学习笔记之 GridView(实现九宫格)
    获取url地址中主机的域名
    C# 语音读取字符串
    JSON省市联动
    MOTO Droid手机自定义本地铃声设置方法
  • 原文地址:https://www.cnblogs.com/youning/p/7197015.html
Copyright © 2020-2023  润新知