1.  集合的概述

     <1>  集合的由来

     在Java程序中,当记录单个的数据时,则声明一个变量

     当记录多个类型相同的数据时,则声明一个(一维)数组

     当记录多个类型不同的数据时,则创建一个对象

     当记录多个类型相同的对象时,则声明一个(一维)对象数组

     当记录多个类型不同的对象时,则创建一个集合

     <2>  集合的框架结构

         Collection: 存放元素的基本单位是单个元素

         Map: 存放元素的基本单位是单对元素 (键值对 <key,value>)

 

2.  Collection 集合

      <1>  基本概念

      <2>  常用方法

          boolean add(E e)  //  增加单个元素

          boolean addAll(Collection<? extends E> c)  //  增加多个元素

          boolean contains(Object o)         //  判断单个元素是否存在

          boolean containsAll(Collection<?> c)  //  判断多个元素是否存在

          boolean retainAll(Collection<?> c)  //  判断多个取交集

          boolean remove(Object o)  //  删除单个

          boolean removeAll(Collection<?> c)  //  删除多个

          void clear()  //  全删

          int size()   //  获取个数 

          boolean isEmpty()  

          boolean equals(Object o)

          int hashCode()

          Object[] toArray()  //  转化为数组

          Iterator iterator()  //  获取迭代器

 

3.  Iterator 接口

    <1>  基本概念 - 用来描述迭代器  

    <2>  常用的方法

          boolean hasNext()  ○ 判断是否有可以访问的元素 

          E next()        ○ 取出元素并指向下一个位置

          void remove()         ○ 删除元素

 

4.  for each 循环

    <1>  基本概念 - for循环的增强版,也是迭代器的简化版

    <2>  语法格式 

        for(元素类型 变量名 :数组 / 结构名){

          语句

        }

    <3>  例题:遍历Collection的三种形式?

         答:1. 使用toString() (整体显示)  2.  使用遍历器  3.  使用for each

 

5.  List集合

    <1> 基本概念

        List是Collection的子集合(子接口),允许有重复的元素,且有先后放入次序

    <2> 主要实现类

        ArrayList  //  数组,擅长访问,不擅长增删

        LinkedList  //  链表,擅长增删,不擅长访问  

        Stack    //  后进先出

        Vector    //  线程安全,效率低

    <3> 常用的方法

        void add (int index, E element)  

          boolean addAll (int index, Collection <? extends E > c)

        E get (int index)

        int indexOf (Object o)

        int lastIndexOf (Object o)

        E set (int index, E element)

        E remove (int index)

        List subList (int fromIndex, int toIndex) 

 

6.  Queue集合

      <0> 先进先出

      <1> 主要实现类 - LinkedList  (在增删方面有优势)

      <2> 常用的方法

          boolean offer (E e)  //  入队

          E poll()  //  出队

          E peek()  //  查看队首元素