Java14-day05【集合(Collection常用方法-遍历、List特有方法、List集合子类特点、LinkedList集合的特有功能、ListIterator)】

目   录

01_集合体系结构

1.1、集合知识回顾

1.2、集合类体系结构

02_Collection集合概述和使用

1.3、Collection集合概述和使用

03_Collection集合常用方法

1.4、Collection集合常用方法

04_Collection集合的遍历

1.5、Collection集合的遍历

05_集合使用步骤图解

1.6、集合的使用步骤图解

06_Collection集合存储学生对象并遍历

案例:Collection集合存储学生对象并遍历

07_List集合概述和特点

2.1、List集合概述和特点

08_List集合的特有方法

2.2、List集合特有方法

09_List集合存储学生对象并遍历

案例:List集合存储学生对象并遍历

10_并发修改异常

2.3、并发修改异常

并发修改异常的源码分析

11_列表迭代器

2.4、ListIterator

ListIterator源码分析

12_增强for循环

2.5、增强for循环

13_List集合存储学生对象三种方式遍历

案例:List集合存储学生对象用三种方式遍历

14_数据结构之栈和队列

2.6、数据结构

2.7、常见数据结构之栈

2.8、常见数据结构之队列

15_数据结构之数组和链表

2.9、常见数据结构之数组

2.10、常见数据结构之链表

16_List集合子类的特点

2.11、List集合子类特点

17_ArrayList集合存储学生对象三种方式遍历

案例:ArrayList集合存储学生对象,用三种方式遍历

18_LinkedList集合的特有功能

2.12、LinkedList集合的特有功能


01_集合体系结构

1.1、集合知识回顾

集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变。

1.2、集合类体系结构

接口不能直接创建对象(实例化),必须通过具体的实现类来创建对象。

02_Collection集合概述和使用

1.3、Collection集合概述和使用

Collection集合概述

  • 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素。
  • JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现。

创建Collection集合的对象

  • 多态的方式
  • 集体的实现类ArrayList

泛型:集合中的元素类型。

03_Collection集合常用方法

1.4、Collection集合常用方法

Alt+7   打开一个窗口,能够看到类的所有信息。

查看ArrayList的add()方法源码:

04_Collection集合的遍历

1.5、Collection集合的遍历

Iterator:迭代器,集合的专用遍历方式

  • Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到。
  • 迭代器是通过集合的iterator()方法得到的,所以说它是依赖于集合而存在的。

Iterator中的常用方法

  • E next():返回迭代中的下一个元素(获取集合元素)。
  • boolean hasNext():如果迭代具有更多元素,则返回 true。

迭代器中的泛型,与集合中的泛型一致。

// Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
Iterator<String> it = c.iterator(); // 多态方式得到Iterator对象 返回Iterator接口的具体实现类:Itr
/*  c.iterator()具体实现方法 --> ArrayList
    public Iterator<E> iterator() { // Iterator接口
        return new Itr(); // 返回Iterator接口的具体实现类对象
    }

    private class Itr implements Iterator<E> { // Itr类 定义在ArrayList中
        ... // ArrayList中的Itr类
    }
 */

 

05_集合使用步骤图解

1.6、集合的使用步骤图解

06_Collection集合存储学生对象并遍历

案例:Collection集合存储学生对象并遍历

07_List集合概述和特点

2.1、List集合概述和特点

List集合概述

  • 有序集合(也称为序列),用户可以精确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素,并搜索列表中的元素。
  • 与Set集合不同,列表通常允许重复的元素。

List集合特点

  • 有序(有索引):存储和取出的元素顺序一致。
  • 可重复:可以存储重复元素。
  • 元素存取有序。

List接口继承自Collection接口,Collection接口中的功能 List接口都可以使用。

08_List集合的特有方法

2.2、List集合特有方法

09_List集合存储学生对象并遍历

案例:List集合存储学生对象并遍历

10_并发修改异常

2.3、并发修改异常

并发修改异常

  • ConcurrentModificationException

产生原因

  • 迭代器遍历的过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断 预期修改值和实际修改值 不一致,则会出现:ConcurrentModificationException。

解决方案

  • 用for循环遍历,然后用集合对象做对应的操作即可。

并发修改异常的源码分析

public interface List<E> {
    Iterator<E> iterator();
    boolean add(E e);
}

public abstract class AbstractList<E> {
    protected int modCount = 0;
}

public class ArrayList<E> extends AbstractList<E> implements List<E> {

    public E get(int index) {
        Objects.checkIndex(index, size);
        return elementData(index);
    }

    public boolean add(E e) {
        modCount++;
        add(e, elementData, size);
        return true;
    }

    public Iterator<E> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<E> {
        int expectedModCount = modCount;
        /*
            modCount:实际修改集合的次数
            expectedModCount:预期修改集合的次数
        */

        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

}

get()方法,不会做“实际修改值 == 预期修改值”的判断。∴,不会出现并发修改异常。

11_列表迭代器

2.4、ListIterator

ListIterator源码分析

public interface List<E> {
    Iterator<E> iterator();
    ListIterator<E> listIterator();
}

public abstract class AbstractList<E> {
    protected int modCount = 0;
}

public class ArrayList<E> extends AbstractList<E> implements List<E> {
    public Iterator<E> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<E> {
        ...
    }

    public ListIterator<E> listIterator() {
        return new ListItr(0);
    }

    private class ListItr extends Itr implements ListIterator<E> {
        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }
}

12_增强for循环

2.5、增强for循环

增强for:简化数组和Collection集合的遍历

  • 实现Iterable接口的类允许其对象成为增强型 for语句的目标。
  • 它是JDK5之后出现的,其内部原理是一个Iterator迭代器。

增强for的格式:

    for(元素数据类型 变量名 : 数组或者Collection集合) {
        // 循环体; // 在此处使用变量即可,该变量就是元素
    }

范例

    int[] arr = {1, 2, 3, 4, 5};
    for(int i: arr) {
        System.out.println(i);
    }

13_List集合存储学生对象三种方式遍历

案例:List集合存储学生对象用三种方式遍历

14_数据结构之栈和队列

2.6、数据结构

数据结构是计算机存储、组织数据的方式。是指相互之间存在一种或多种特定关系的数据元素的集合。

通常情况下,精心选择的数据结构可以带来更高的运行或者存储效率。

2.7、常见数据结构之栈

2.8、常见数据结构之队列

15_数据结构之数组和链表

2.9、常见数据结构之数组

2.10、常见数据结构之链表

16_List集合子类的特点

2.11、List集合子类特点

List集合常用子类:ArrayList、LinkedList

ArrayList  集合:底层是数组结构实现,查询快、增删慢 

LinkedList集合:底层是链表结构实现,查询慢、增删快 

练习:分别使用ArrayList和LinkedList完成存储字符串并遍历。

 

package com.itheima_07;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;

public class ListDemo {
    public static void main(String[] args) {
        //创建集合对象
        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("hello");
        arrayList.add("world");
        arrayList.add("java");

        // 1、迭代器:集合特有的遍历方式
        Iterator<String> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        System.out.println("----------------");

        // 2、普通for:带有索引的遍历方式
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
        System.out.println("----------------");

        // 3、增强for:最方便的遍历方式
        for (String s : arrayList) {
            System.out.println(s);
        }
        System.out.println("----------------");

        LinkedList<String> linkedList = new LinkedList<String>();

        linkedList.add("hello2");
        linkedList.add("world2");
        linkedList.add("java2");

        // 1、迭代器:集合特有的遍历方式
        Iterator<String> iterator2 = linkedList.iterator();
        while (iterator2.hasNext()) {
            System.out.println(iterator2.next());
        }
        System.out.println("--------");

        // 2、普通for:带有索引的遍历方式
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.println(linkedList.get(i));
        }
        System.out.println("--------");

        // 3、增强for:最方便的遍历方式
        for (String s : linkedList) {
            System.out.println(s);
        }
        System.out.println("--------");
    }
}

17_ArrayList集合存储学生对象三种方式遍历

案例:ArrayList集合存储学生对象,用三种方式遍历

18_LinkedList集合的特有功能

2.12、LinkedList集合的特有功能

猜你喜欢

转载自blog.csdn.net/weixin_44949135/article/details/107709208