Collection集合,List集合

一、Collection集合

01 集合体系结构

  a:Collection 集合(单身汉集合)
      |_ List(特点:有序,可重复,有索引)
        |_ ArrayList(重点掌握)
        |_ LinkedList(存储的元素不可重复,存取顺序一致)

      |_ Set(无序,元素不可重复,元素无索引)
        |_ HashSet(重点掌握)
        |_ LinkedHashSet
        |_ TreeSet 

  b:Map集合(夫妻对集合)
    |_ HashMap(重点掌握)
    |_ LinkedHashMap
    |_ Hashtable
    	|_ Properties(重点掌握)
    	Collection集合基本使用:
    Collection<元素的类型> col = new ArrayList<>();

2.Collection集合的常用方法【应用】

* boolean add(E e)          	添加元素   永远返回的都是true  可以添加重复的内容           
* boolean remove(Object o)  	从集合中移除指定的元素      
* void    clear()            	清空集合中的元素         
* boolean contains(Object o)	判断集合中是否存在指定的元素   
* boolean isEmpty()         	判断集合是否为空         
* int     size()              	集合的长度,也就是集合中元素的个数

3.Collection集合的遍历

  • 迭代器的介绍
    迭代器,集合的专用遍历方式
    Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
    迭代器是通过集合的iterator():方法得到的,所以我们说它是依赖于集合而存在的
    例题:
public static void main(String[] args) {
          Collection<String> coll = new ArrayList<>();

          coll.add("宝宝");
          coll.add("贝贝");
          coll.add("乖");
			//进入循环前先使用迭代器遍历
          Iterator<String> it = coll.iterator();
			//判断如果有值就进入循环
          while (it.hasNext()) {
              String next = it.next();
              System.out.println(next);
          }

          System.out.println(coll);
      }

4.集合使用步骤图解:
第一步: 创建集合(该如何选择集合)
第二步: 添加元素
* 创建元素
* 将元素添加到集合中
第三步: 遍历集合
* 迭代器
* 获取迭代器
* 使用hasNext() 方法判断是否有元素
* 使用next() 方法来获取元素

集合案例:

案例需求
  	创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合

  //案例代码:
    public class CollectionDemo03 {
    public static void main(String[] args) {
        // 创建集合 ArrayList
        // ArrayList list = new ArrayList<>();
        Collection<Student> list = new ArrayList<>();

        list.add(new Student("蓉蓉",39));
        list.add(new Student("宋吉吉",40));
        list.add(new Student("陶吉吉",35));

    // 迭代器
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            System.out.println(stu);
        }
    }
}

二、List集合
1.List集合概述和特点
总结:
* List集合特点
* 有序: 存储和取出的顺序是一样的
* 可重复: 可以存储重复的元素
* 有索引: 提供了整数的索引
2.List(ArrayList)集合的特有方法【应用】 以前学ArrayList集合的时候学的方法:

  • void add(int index,E element) 在此集合中的指定位置插入指定的元素
  • E remove(int index) 删除指定索引处的元素,返回被删除的元素
  • E set(int index,E element) 修改指定索引处的元素,`返回被修改的元素
  • E get(int index) 返回指定索引处的元素
    List集合的案例:
案例需求
     创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
 //案例代码:
	public class CollectionDemo03 {
    public static void main(String[] args) {
        // 创建集合 
        List<Student> list = new ArrayList<>();

        list.add(new Student("蓉蓉",39));
        list.add(new Student("宋吉吉",40));
        list.add(new Student("陶吉吉",35));

    // 迭代器
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            System.out.println(stu);
        }
    }
}

三、LinkedList集合的特有功能

  • 特有方法
    • public void addFirst(E e) 在该列表开头插入指定的元素
    • public void addLast(E e) 将指定的元素追加到此列表的末尾
    • public E getFirst() 返回此列表中的第一个元素
    • public E getLast() 返回此列表中的最后一个元素
    • public E removeFirst() 从此列表中删除并返回第一个元素
    • public E removeLast() 从此列表中删除并返回最后一个元素

你可能感兴趣的:(JAVA)