zoukankan      html  css  js  c++  java
  • 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
    
    
            
            
            
    
    
    
            
    
        }
    
    
    }
  • 相关阅读:
    【转】c++继承中的内存布局
    Google 开源项目风格指南
    常见面试题
    PHP7.1中使用openssl替换mcrypt
    phpunit实践笔记
    PHP的错误处理
    CI的扩展机制
    #CI的MVC实现
    Laravel中的队列处理
    laravel的模块化是如何实现的
  • 原文地址:https://www.cnblogs.com/youning/p/7197015.html
Copyright © 2011-2022 走看看