• Collection集合 迭代器 增强for List集合 LinkedList集合详解


    1.Collection集合

    1.1数组和集合的区别【理解】

    相同点
    都是容器,可以存储多个数据。
    不同点
    数组的长度是不可变的,集合的长度是可变的。
    数组可以存基本数据类型和引用数据类型。
    集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类。

    1.2集合类体系结构【理解】

    在这里插入图片描述

    1.3 Collection 集合概述和使用

    Collection集合概述
    1.是单列集合的顶层接口,它表示一组对象,这些对象也称为 Collection的元素。
    2.JDK不提供此接口的任何直接实现,它提供更具体的子接口(如 Set 和 List)实现。
    创建 Collection 集合的对象
    1.多态的方式
    2.具体的实现类 ArrayList
    Collection 集合常用的方法

    方法名说明
    boolean add(E e)添加元素
    boolean remove(Object o)从集合中移除指定的元素
    boolean removeif(Object o)根据条件进行删除
    void clear()清空集合中的元素
    boolean contains(Object o)判断集合中是否存在指定的元素
    boolean isEmpty()判断集合是否为空
    int size()集合的长度,也就是集合中元素的个数

    1.4Collection集合的遍历【应用】

    1.4.1迭代器介绍

    	1.迭代器,集合的专用遍历方式。
    	2.Iterator iterator():返回此集合中元素的迭代器,通过集合对象的 iterator()方法得到。
    
    • 1
    • 2

    1.4.2 Iterator 中的常用方法

    方法名说明
    boolean hasNext()判断当前位置是否有元素可以被取出
    E next()获取当前位置的元素,将迭代器对象移向下一个索引的位置

    1.4.3 Collection 集合的遍历

    public class IteratorDemo1 {
    	public static void main(String[] args){
    	//创建集合对象
    		Collection<String> collection = new ArrayList<>();//集合的多态形式
    	//添加元素
    	collection.add("hello")
    	collection.add("word")
    	collection.add("java")
    	collection.add("javaee")
    
    	//Iteratoriterator():返回此集合中元素的迭代器,通过集合的 iterator()方法得到
    	Irerator it = collection.iterator();
    
    	//用 while 循环改进元素的判断和获取
    		while(it.hasNext()){
    			String s = it.next();
    			System.out.println(s);
    		}
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    1.4.4迭代器中删除的方法

    void remove():	删除迭代器对象当前指向的元素
    
    • 1
    public class IteratorDemo2 {
    	public static void main(String[] args) {
    		ArrayList<String> list = new ArrayList<>();
    		list.add("a");
    		list.add("b");
    		list.add("b");
    		list.add("c");
    		list.add("d");
    		Iterator it = list.iterator();
    		while(it.hasNext()){
    			String s = it.next();
    			if(b.equals(s)){
    			//指向谁那么此时就删除谁
    			it.remove();
    			}
    		}
    		System.out.println(list);
    	}
    }
    		
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1.4.5 增强 for 循环

    介绍
    1.它是 JDK5 之后出现的,其内部原理是一个 Iterator 迭代器。
    2.实现 Iterable 接口的类才可以使用迭代器和增强 for。
    3.简化数组和 Collection 集合的遍历。
    格式
    for(集合/数组中元素的数据类型 变量名 : 集合/数组名) {
    // 已经将当前遍历到的元素封装到变量中了,直接使用变量即可
    }
    代码

    public class MyCollectionDemo1{
    	public static void main(String[]args){
    	ArrayList<String> list = new ArrayList<>();
    		list.add("a"); 
    		list.add("b"); 
    		list.add("c"); 
    		list.add("d");
    		 list.add("e"); 
    		 list.add("f");
    		 
    		 //1,数据类型一定是集合或者数组中元素的类型 
    		 //2,str仅仅是一个变量名而已,在循环的过程中,依次表示集合或者数组中的每一个元素 
    		 //3,list就是要遍历的集合或者数组
    		for(String s:list){
    			System.out.println(s);
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2. List集合

    2.1 List集合的概述和特点【记忆】

    2.1.1 List集合的概述

    	1.有序集合,这里的有序指的是存取顺序
    	2.用户可以精确控制列表中每个元素的插入位置,用户可以通过整数索引访问元素,
    	并搜索列表中的元素。
    	3.与 Set 集合不同,列表中通常允许重复的元素
    
    • 1
    • 2
    • 3
    • 4

    2.1.2 List集合的特点

    	1.存取有序
    	2.可以重复
    	3.有索引
    
    • 1
    • 2
    • 3

    2.2 List 集合的特有方法

    方法说明
    void add(int index, E element)在此集合中指定位置插入指定的元素
    E remove(int index)删除指定索引处的元素,返回被删除的元素
    E set(int index, E element)修改指定索引处的的元素,返回被修改的元素
    E get(index)返回指定索引处的元素

    3.数据结构

    3.1数据结构之栈和队列【记忆】

    栈结构特点:先进后出
    队列结构特点:先进先出
    
    • 1
    • 2

    3.2数据结构之数组和链表【记忆】

    数组结构:查询快、增删慢
    队列结构:查询慢,增删快
    
    • 1
    • 2

    4.List集合的实现类

    4.1 List 集合子类的特点【记忆】

    ArrayList集合
    底层是数组结构,查询快,增删慢
    LinkedList集合
    底层是链表结构实现,查询慢,增删快

    4.2 LinkedList 集合的特有功能【应用】

    特有方法

    方法名说明
    public void addFirst(E e)在该列表开头插入指定的元素
    public void addLast(E e)将指定的元素追加到此列表的末尾
    public E getFirst()返回此列表中的第一个元素
    public E getLast()返回此列表的最后一个元素
    public E removeFirst()从此列表中删除并返回第一个元素
    public E removeLast()从此列表中删除并返回最后一各元素
  • 相关阅读:
    最优秀的完整的数字音频工作站水果音乐FL Studio21.1.1.3750中文解锁版
    如何建立完整的、有效的会员体系?
    Iocomp Components Full Sources Product
    【Python机器学习】自动化特征选择——迭代特征选择
    实战:自定义简单版redux, enhancer与react-redux
    【剑指】数组中的重复数字
    进阶:Python序列的修改、散列和切片
    Figma UI UX设计教程
    python的str.find()用法及实例
    真实的博弈智能可能不太讲究鲁棒性
  • 原文地址:https://blog.csdn.net/hihielite/article/details/126861974