zoukankan      html  css  js  c++  java
  • Day15_Collection集合

    集合

    集合的概念

    对象的容器,实现了对对象的常用的操作,类似数组功能

    • 和数组的区别:

      • (1)数组的长度固定,几个长度不固定
      • (2)数组可以存储基本类型和引用类型,集合只能存储引用类型
    • 位置:java.util.*

    Collection父接口

    • 特点:代表一组任意类型的对象,无序、无下标、不能重复

    • 方法:

      • boolean add(0bject obj)//添加一个对象。
      • boolean addAll(Collection c)//将一个集合中的所有对象添加到此集合中。
      • void clear()//清空此集合中的所有对象。
      • boolean contains (Object o)//检查此集合中是否包含o对象
      • boolean equals(Object o)//比较此集合是否与指定对象相等。
      • boolean isEmpty )//判断此集合是否为空
      • boolean remove (Object o)//在此集合中移除o对象
      • int size()//返回此集合中的元素个数。
      • 0bject[] toArray()//将此集合转换成数组。
    package com.oop.Demo12;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Iterator;
    
    /**
     * Collection类
     * @author lemon
     */
    public class Demo03 {
        public static void main(String[] args) {
            //创建集合
            Collection collection=new ArrayList ();
            //添加元素
            collection.add ("apple");
            collection.add ("pear");
            collection.add ("lemon");
            System.out.println ("数组个数为:"+collection.size ());
            System.out.println (collection);
            //删除元素
    //        collection.remove ("apple");
    //        System.out.println (collection);
    //        collection.clear ();
    //        System.out.println ("数组个数为:"+collection.size ());
            //(3)遍历数组
            //3.1使用增强for
            System.out.println ("-------3.1使用增强for--------");
            for (Object object:collection) {
                System.out.println (object);
            }
            //3.2使用迭代器(迭代器专门用来遍历集合的一种方式)
            System.out.println ("------3.2使用迭代器-------");
            Iterator it = collection.iterator ();
            while (it.hasNext ()){
                String s=(String) it.next ();
                System.out.println (s);
                //collection.remove (s);//ConcurrentModificationException抛异常,运行过程不允许使用其他方法
                //it.remove ();//可以用迭代器的方法/元素个数:0
            }
            System.out.println ("元素个数:"+collection.size ());
            //(4)判断
            System.out.println (collection.contains ("lemon"));
            System.out.println (collection.isEmpty ());
        }
    }
    
    //运行结果
    数组个数为:3
    [apple, pear, lemon]
    -------3.1使用增强for--------
    apple
    pear
    lemon
    ------3.2使用迭代器-------
    apple
    pear
    lemon
    元素个数:3
    true
    false
    
    Process finished with exit code 0
    
    package com.oop.Demo12;
    
    import java.util.ArrayList;
    import java.util.Collection;
    
    /**
     * Collection
     */
    public class Demo04 {
        public static void main(String[] args) {
            Collection collection=new ArrayList ();
            Student student1= new Student ("张三",19);
            Student student2= new Student ("李四",11);
            Student student3= new Student ("王五",20);
            //1.添加数据
            collection.add (student1);
            collection.add (student2);
            collection.add (student3);
            System.out.println (collection.size ());
            System.out.println (collection.to
            String ());
        }
    }
    
    3
    [Student{name='张三', age=19}, Student{name='李四', age=11}, Student{name='王五', age=20}]
    
    Process finished with exit code 0
    
  • 相关阅读:
    C# 中字符串转换成日期
    c#中退出WinForm程序包括有很多方法,如:this.Close(); Application.Exit();Application.ExitThread(); System.Environment.Exit(0);
    c#获取程序版本号
    分分钟用上C#中的委托和事件
    【转载】C# 中的委托和事件(详解:简单易懂的讲解)
    C#什么时候需要使用构造函数
    15、生命周期-BeanPostProcessor-后置处理器
    13、生命周期-InitializingBean和DisposableBean
    11、组件注册-使用FactoryBean注册组件
    12、生命周期-@Bean指定初始化和销毁方法
  • 原文地址:https://www.cnblogs.com/lemonlover/p/14067794.html
Copyright © 2011-2022 走看看