当前位置:网站首页>(九)集合 - List
(九)集合 - List
2022-08-06 03:33:00 【来世做春风嘛】
一、集合框架体系
集合:
1)可以动态保存任意多个对象。
2)提供了一系列方便的操作对象的方法:add、remove、set、get等
二、Collection
1. Collection 接口常用方法
1)add:添加单个元素
2)remove:删除指定元素
3)contains:查找元素是否存在
4)size:获取元素个数
5)isEmpty:判断是否为空
6)clear:清空
7)addAll:添加多个元素
8)containsAIl:查找多个元素是否都存在
9)removeAll:删除多个元素
2. Collection 接口遍历元素方式1,使用 Iterator(迭代器)
1)Iterator 对象称为迭代器,主要用于遍历 Collection集合 中的元素
2)所有实现了 Collection接口 的集合类都有一个 iterator() 方法,用以返口一个实现了Iterator接口 的对象,即可以返回一个迭代器。
3)Iterator 仅用于遍历集合,lterator 本身并不存放对象
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); // list.iterator() 重置迭代器,可重复 Iterator<Integer> iterator = list.iterator(); while (iterator.hasNext()){ Integer next = iterator.next(); System.out.println(next); }
3. Collection 接口遍历元素方式2,for循环增强
增强for循环,可以代替 iterator迭代器,特点:增强for 本质就是迭代器。只能用于遍历集合或数组。
三、List
1. List 接口基本介绍
1)List 接口是 Collection 接口的子接口
2)List 集合类中元素有序(即添加顺序和取出顺序一致)、且可重复
3)List集合中的每个元素都有其对应的顺序索引,即支持索引。
4)List 容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素
2. List 集合里添加了一些根据索引来操作集合元素的方法
1)void add (int index, Object ele):在 index 位置插入 ele 元素
2)boolean addAll (int index, Collection eles):从 index 位置开始将 eles 中的所有元素添加进来
3)Object get (int index):获取指定 index 位置的元素
4)int indexOf (Object obj):返回 obj 在集合中首次出现的位置
5)int lastlndexOf (Object obj):返回 obj 在当前集合中末次出现的位置
6)Object remove (int index):移除指定 index 位置的元素,并返回此元素
7)Object set (int index, Object ele):设置指定 index 位置的元素为 ele,相当于是替换
8)List subList (int fromlndex,int tolndex):返回从 fromlndex 到(tolndex -1)位置的子集合
List<String> list = new ArrayList<>(); list.add("乔峰"); list.add("段誉"); list.add(1, "虚竹"); list.add("Tom"); System.out.println(list); // [乔峰, 虚竹, 段誉, Tom] List<String> list2 = new ArrayList<>(); list2.add("Jack"); list2.add("Tom"); list.addAll(1, list2); System.out.println(list); // [乔峰, Jack, Tom, 虚竹, 段誉, Tom] System.out.println(list.get(1)); // Jack System.out.println(list.indexOf("Tom")); // 2 System.out.println(list.lastIndexOf("Tom")); // 5 list.remove(5); System.out.println(list); // [乔峰, Jack, Tom, 虚竹, 段誉] list.set(2, "Mike"); System.out.println(list); // [乔峰, Jack, Mike, 虚竹, 段誉] System.out.println(list.subList(0, 2)); // [乔峰, Jack]
四、ArrayList(P509)
1. ArrayList的注意事项
1)ArrayList 可以加入null,并且多个
2)ArrayList 是由数组来实现数据存储的
3)ArrayList 基本等同于Vector。ArrayList是线程不安全(执行效率高),在多线程情况下,不建议使用ArrayList。
2. ArrayList的底层操作机制源码分析(P510)
(1)ArrayList 中维护了一个 Object 类型的数组 elementData
transient Objectt[] elementData;【transient 表示该属性不会被序列化】
(2)当创建 ArrayList 对象时,如果使用的是无参构造器,则初始 elementData 容量为0。
第一次添加,则扩容 elementData 为10。如果需要再次扩容的话,则扩容 elementData 为1.5倍。
(3)如果使用的是指定大小的构造器,则初始 elementData 容量为指定大小,如果需要扩容,则直接扩容 elementData 为1.5倍。
五、Vector(P513)
1. Vector 的基本介绍
1)Vector 类的定义说明
public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
2)Vector 底层也是一个对象数组 protected Object[] elementData;
3)Vector 是线程同步的,即线程安全,Vector 类的操作方法带有 synchronized
4)在开发中,需要线程同步安全时,考虑使用 Vector
六、LinkedList(P515)
1. LinkedList 说明
1)LinkedList 底层实现了双向链表和双端队列特点
2)可以添加任意元素(元素可以重复),包括 null
3)线程不安全,没有实现同步
2. LinkedList 的底层操作机制
1)LinkedList 底层维护了一个双向链表
2)LinkedList 中维护了两个属性 first 和 last 分别指向 首节点和 尾节点
3)每个节点(Node对象),里面又维护了 prev 、next 、item 三个属性,其中通过 prev 指向前一个,通过 next 指向后一个节点。最终实现双向链表
4)所以 LinkedList 的元素的添加和删除,不是通过数组完成的,相对来说效率较高
3. LinkedList 源码解读(P516)
七、ArrayList和LinkedList比较(P517)
1)如果我们改查的操作多,选择 ArrayList
2)如果我们增删的操作多,选择 LinkedList
3)一般来说,在程序中,80%-90%都是查询,因此大部分情况下会选择 ArrayList
4)在一个项目中,根据业务灵活选择,也可能这样,一个模块使用的是ArrayList,另
外一个模块是LinkedList。
边栏推荐
- 程序员上班第一天 | 每日趣闻
- 造自己的芯,让谷歌买单!谷歌再度开源 180nm 工艺的芯片
- 入坑机器学习:二,监督学习
- Use BeanUtils with caution, the performance really stretches!
- Wang Qi, Secretary of the Party Committee and President of Shanghai Pudong Development Bank Changsha Branch, and Jiang Junwen, President of Huarong Xiangjiang Bank, visited Kylin Principal for investi
- Web3.exceptions.ExtraDataLengthError
- 【LaTex】 - How to use the alignment symbol &, the usage of the newline character \\, how to solve the Misplaced & error
- 2022 Alibaba Cloud server configuration selection strategy
- ctf (finalrec)
- Mysql安装 求大拿解答
猜你喜欢
预处理(C语言深度了解)
leetcode 15. Sum of three numbers
Which browser has fewer ads?Multi-Royal Safe Browser Light Experience
Single player up to 50,000 bonus!The top conference paper recurrence competition is officially launched, and 70+ public tasks are waiting for you to challenge
【wpf】依赖属性三个回调详解
ALV细节再梳理2022.8.5
学习MySQL的第一天:MySQL概述(基础篇)
Failed to save image in R language ggplot2 loop
Advantages and disadvantages of solid tires
xctf攻防世界 Web高手进阶区 upload1
随机推荐
Mysql安装 求大拿解答
物联网协议概述
【无标题】
使用 Monaco Editor 开发 SQL 编辑器
【Untitled】
Lvm root partition expansion
泡沫填充轮胎
2022年天猫七夕价格便宜吗?天猫七夕节有什么优惠?
深度学习Course2第二周Hyperparameter tuning, Batch Normalization, Programming Frameworks习题整理
C Student Management System Print/Modify Designated Location Information
2018HBCPC部分题解
2022 使用Go语言构建现代 Web 应用程序实战内容课程
学习MySQL的第一天:MySQL概述(基础篇)
Induction of common annotations in Sring
Failed to save image in R language ggplot2 loop
自定义View,和Canvas(画布),Paint(画笔),Path(路径)的用法
[Deep Learning 21 Days Learning Challenge] Memo: Model Reuse - Model Saving and Loading
ALV细节再梳理2022.8.5
2022年天猫8月份有什么大的活动?
【wpf】依赖属性三个回调详解