CollenctionList

article/2025/11/5 8:17:08

1.Collection集合

1.1集合体系结构【记忆】

  • 集合类的特点

提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变

  • 集合类的体系图

1.2Collection集合概述和基本使用【应用】

  • Collection集合概述

  • 是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素

  • JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现

  • Collection集合基本使用

public class CollectionDemo01 {public static void main(String[] args) {//创建Collection集合的对象Collection<String> c = new ArrayList<String>();//添加元素:boolean add(E e)c.add("hello");c.add("world");c.add("java");//输出集合对象System.out.println(c);}
}

1.3Collection集合的常用方法【应用】

方法名

说明

boolean add(E e)

添加元素

boolean remove(Object o)

从集合中移除指定的元素

void clear()

清空集合中的元素

boolean contains(Object o)

判断集合中是否存在指定的元素

boolean isEmpty()

判断集合是否为空

int size()

集合的长度,也就是集合中元素的个数

1.4Collection集合的遍历【应用】

  • 迭代器的介绍

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

  • Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到

  • 迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的

  • Collection集合的遍历

public class IteratorDemo {public static void main(String[] args) {//创建集合对象Collection<String> c = new ArrayList<>();//添加元素c.add("hello");c.add("world");c.add("java");c.add("javaee");//Iterator<E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到Iterator<String> it = c.iterator();//用while循环改进元素的判断和获取while (it.hasNext()) {String s = it.next();System.out.println(s);}}
}

1.5集合使用步骤图解【理解】

  • 使用步骤

1.6集合的案例-Collection集合存储学生对象并遍历【应用】

  • 案例需求

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

  • 代码实现

  • 学生类

public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
  • 测试类

public class CollectionDemo {public static void main(String[] args) {//创建Collection集合对象Collection<Student> c = new ArrayList<Student>();//创建学生对象Student s1 = new Student("林青霞", 30);Student s2 = new Student("张曼玉", 35);Student s3 = new Student("王祖贤", 33);//把学生添加到集合c.add(s1);c.add(s2);c.add(s3);//遍历集合(迭代器方式)Iterator<Student> it = c.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName() + "," + s.getAge());}}
}

2.List集合

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

  • List集合概述

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

  • 与Set集合不同,列表通常允许重复的元素

  • List集合特点

  • 有索引

  • 可以存储重复元素

  • 元素存取有序

2.2List集合的特有方法【应用】

方法名

描述

void add(int index,E element)

在此集合中的指定位置插入指定的元素

E remove(int index)

删除指定索引处的元素,返回被删除的元素

E set(int index,E element)

修改指定索引处的元素,返回被修改的元素

E get(int index)

返回指定索引处的元素

2.3集合的案例-List集合存储学生对象并遍历【应用】

  • 案例需求

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

  • 代码实现

  • 学生类

public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
  • 测试类

public class ListDemo {public static void main(String[] args) {//创建List集合对象List<Student> list = new ArrayList<Student>();//创建学生对象Student s1 = new Student("林青霞", 30);Student s2 = new Student("张曼玉", 35);Student s3 = new Student("王祖贤", 33);//把学生添加到集合list.add(s1);list.add(s2);list.add(s3);//迭代器方式Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName() + "," + s.getAge());}System.out.println("--------");//for循环方式for(int i=0; i<list.size(); i++) {Student s = list.get(i);System.out.println(s.getName() + "," + s.getAge());}}
}

2.4并发修改异常【应用】

  • 出现的原因

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

  • 解决的方案

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

  • 示例代码

public class ListDemo {public static void main(String[] args) {//创建集合对象List<String> list = new ArrayList<String>();//添加元素list.add("hello");list.add("world");list.add("java");//遍历集合,得到每一个元素,看有没有"world"这个元素,如果有,我就添加一个"javaee"元素,请写代码实现
//        Iterator<String> it = list.iterator();
//        while (it.hasNext()) {
//            String s = it.next();
//            if(s.equals("world")) {
//                list.add("javaee");
//            }
//        }for(int i=0; i<list.size(); i++) {String s = list.get(i);if(s.equals("world")) {list.add("javaee");}}//输出集合对象System.out.println(list);}
}

2.5列表迭代器【应用】

  • ListIterator介绍

  • 通过List集合的listIterator()方法得到,所以说它是List集合特有的迭代器

  • 用于允许程序员沿任一方向遍历的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置

  • 示例代码

public class ListIteratorDemo {public static void main(String[] args) {//创建集合对象List<String> list = new ArrayList<String>();//添加元素list.add("hello");list.add("world");list.add("java");//获取列表迭代器ListIterator<String> lit = list.listIterator();while (lit.hasNext()) {String s = lit.next();if(s.equals("world")) {lit.add("javaee");}}System.out.println(list);}
}
  • ListIterator 常用方法

方法名

描述

E next()

返回列表中的下一个元素

boolean hasNext()

如果迭代具有更多元素,则返回 true

E previous()

返回列表中的上一个元素

boolean hasPrevious()

如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true

void add(E e)

将指定的元素插入

2.6增强for循环【应用】

  • 定义格式

for(元素数据类型 变量名 : 数组/集合对象名) {循环体;
}
  • 示例代码

public class ForDemo {public static void main(String[] args) {int[] arr = {1,2,3,4,5};for(int i : arr) {System.out.println(i);}System.out.println("--------");String[] strArray = {"hello","world","java"};for(String s : strArray) {System.out.println(s);}System.out.println("--------");List<String> list = new ArrayList<String>();list.add("hello");list.add("world");list.add("java");for(String s : list) {System.out.println(s);}System.out.println("--------");//内部原理是一个Iterator迭代器/*for(String s : list) {if(s.equals("world")) {list.add("javaee"); //ConcurrentModificationException}}*/}
}

2.7集合的案例-List集合存储学生对象三种方式遍历【应用】

  • 案例需求

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

  • 代码实现

  • 学生类

public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
  • 测试类

public class ListDemo {public static void main(String[] args) {//创建List集合对象List<Student> list = new ArrayList<Student>();//创建学生对象Student s1 = new Student("林青霞", 30);Student s2 = new Student("张曼玉", 35);Student s3 = new Student("王祖贤", 33);//把学生添加到集合list.add(s1);list.add(s2);list.add(s3);//迭代器:集合特有的遍历方式Iterator<Student> it = list.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName()+","+s.getAge());}System.out.println("--------");//普通for:带有索引的遍历方式for(int i=0; i<list.size(); i++) {Student s = list.get(i);System.out.println(s.getName()+","+s.getAge());}System.out.println("--------");//增强for:最方便的遍历方式for(Student s : list) {System.out.println(s.getName()+","+s.getAge());}}
}

3.数据结构

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

  • 栈结构

先进后出

  • 队列结构

先进先出

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

  • 数组结构

查询快、增删慢

  • 队列结构

查询慢、增删快

4.List集合的实现类

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

  • ArrayList集合

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

  • LinkedList集合

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

4.2集合的案例-ArrayList集合存储学生对象三种方式遍历【应用】

  • 案例需求

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

  • 代码实现

  • 学生类

public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
  • 测试类

public class ArrayListDemo {public static void main(String[] args) {//创建ArrayList集合对象ArrayList<Student> array = new ArrayList<Student>();//创建学生对象Student s1 = new Student("林青霞", 30);Student s2 = new Student("张曼玉", 35);Student s3 = new Student("王祖贤", 33);//把学生添加到集合array.add(s1);array.add(s2);array.add(s3);//迭代器:集合特有的遍历方式Iterator<Student> it = array.iterator();while (it.hasNext()) {Student s = it.next();System.out.println(s.getName() + "," + s.getAge());}System.out.println("--------");//普通for:带有索引的遍历方式for(int i=0; i<array.size(); i++) {Student s = array.get(i);System.out.println(s.getName() + "," + s.getAge());}System.out.println("--------");//增强for:最方便的遍历方式for(Student s : array) {System.out.println(s.getName() + "," + s.getAge());}}
}

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

  • 特有方法

方法名

说明

public void addFirst(E e)

在该列表开头插入指定的元素

public void addLast(E e)

将指定的元素追加到此列表的末尾

public E getFirst()

返回此列表中的第一个元素

public E getLast()

返回此列表中的最后一个元素

public E removeFirst()

从此列表中删除并返回第一个元素

public E removeLast()

从此列表中删除并返回最后一个元素


http://chatgpt.dhexx.cn/article/Ag3pU3or.shtml

相关文章

输入界面,关于stretchColumns和selectAllOnFocus的属性设置

这是整个TableLayout的代码&#xff1a; <TableLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:orientation"vertical"android:layout_width"fill_parent&qu…

android:stretchcolumns=0,1,2,3,stretch_stretch是什么意思

stretch是什么意思 stretch是伸展、可伸缩的意思。具体释义如下&#xff1a; stretch英 [stretʃ] 美 [strɛtʃ] 1、动词 v.伸展;延伸;持续;包括 例&#xff1a;It is better to stretch the tight muscles first 最好先伸展一下僵硬的肌肉。 2、名词 n.伸展;弹性;一片;一…

StretchBlt()函数使用

StretchBlt函数从源矩形中复制一个位图到目标矩形&#xff0c;必要时按目前目标设备设置的模式进行图像的拉伸或压缩。 说白了功能就是缩放。 函数原型如下 函数原型&#xff1a;BOOL StretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeig…

STL_set/multiset

STL_set/multiset 简介&#xff1a;本文主要介绍STL中的&#xff0c;set与multiset的使用&#xff0c;只需要把本文的代码自己敲完便可学会。 set容器的基本概念 注意&#xff1a;set容器没有push_back, pop_back这两种插入接口&#xff0c;只能用insert函数进行插入 如果向s…

Column-Stores vs. Row-Stores: How Different Are They Really?

Column-Stores vs. Row-Stores: How Different Are They Really? 论文阐述的就是行存与列存 两者之间到底有什么区别 Abstract 论文首先给出结论&#xff1a;列式存储&#xff08;Column Stores&#xff09;比行式存储&#xff08;Row Stores&#xff09;在性能上好过一个数…

android:stretchcolumns=0,1,2,3,android:stretchColumns用法

TableLayout是一个使用复杂的布局&#xff0c;最简单的用法就仅仅是拖拉控件做出个界面&#xff0c;但实际上&#xff0c;会经常在代码里使用TableLayout&#xff0c;例如做出表格的效果。本文主要介绍TableLayout的基本使用方法。 < ?xml version"1.0" encoding…

cannot set a row with mismatched columns

错误&#xff1a;cannot set a row with mismatched columns 错误背景原错误情况错误原因解决方法 错误背景 在希望将dataframe a 中的特定行移至dataframe b 时出错&#xff0c;记录下自己使用的方法 原错误情况 #dataframe a 已知 a{ab:[1,2,3],bb:[3,4,5],cb:[4,5,6]} a …

TableLayout中stretchColumns、shrinkColumns的用法

android:stretchColumns"1" android:shrinkColumns"1"这两个属性是TableLayout所特有的&#xff0c;也是这两个属性影响了子对象的布局。 表格布局是按照行列来组织子视图的布局。表格布局包含一系列的Tabrow对象&#xff0c;用于定义行&#xff08;也可以…

TableLayout中stretchColumns和shrinkColumns使用

<?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"android:layout_height"match_parent"android:orientatio…

基于JAVA的即时通信软件

一.设计任务书 1.1 设计任务 本文设计的是一个简单的即时通信软件&#xff0c;利用 Java Socket 进行点到点通信&#xff0c;其工作机制模仿即时通信软件的基本功能&#xff0c;已实现的功能有&#xff1a; 客户端登录 客户端退出 群组成员之间传输文字或图片信息 该软件分为客…

一文教你用java实现即时通讯软件的设计

即时通讯软件即所谓的聊天工具&#xff0c;其主要用途是用于文字信息的传递与文件传输。使用eclipse作为即时通讯软件的开发工具&#xff0c;使用Socket建立通讯渠道&#xff0c;多线程实现多台计算机同时进行信息的传递&#xff0c;swing技术等进行实际开发相对比较合适。通过…

即时通讯软件七大优势详解

在以前&#xff0c;即时通讯软件被认为是一种专供个人使用的通信工具&#xff0c;随着社会的发展&#xff0c;各大商家企业开始慢慢接受这种通信工具并利用其协调公司内部沟通&#xff0c;从而满足公司的业务需求。那么即时通讯软件优势有哪些&#xff1f;为你细数它的七大优势…

开发IM即时通讯容易吗?需要什么技术

即时通讯现在已经随着互联网技术的应用走进了千家万户&#xff0c;跟早些年的通信工具不同&#xff0c;现在的即时通讯技术已经涵盖了语音即时通讯、视频即时通讯、文字即时通讯等多种方式&#xff0c;而开发即时通讯也成了很多互联网企业投身这一行业后想要尝试的内容。开发即…

桌面软件开发框架大赏

本文基于海康威视桌面端技术专家刘晓伦在「RTC Dev Meetup • 杭州站丨大前端时代的业务架构和跨端实践」活动中分享内容二次整理。 以下正文&#xff1a; 今天要与大家分享 19 款桌面软件开发框架&#xff0c;我将它们分了四类&#xff0c;然后分别就每个类别做相应的介绍&a…

仿QQ即时通讯软件开发-赖国荣-专题视频课程

仿QQ即时通讯软件开发—7495人已学习 课程介绍 会使用JAVA的Swing做UI&#xff0c;学会用JAVA操作数据库&#xff0c;用Java的网络编程&#xff0c;多线程编程&#xff0c;制作一个仿QQ的即时通讯软件&#xff0c;实现在局域网或者互联网通讯 课程收益 制作仿QQ即时通讯软件…

Qt制作局域网即时通讯软件

Qt制作局域网即时通讯软件 利用Qt制作的局域网即时通信软件&#xff0c;可实现文本信息、表情包、图片、文档等的传输功能。界面风格模仿的Tim&#xff0c;所以本软件取名为Timi&#xff0c;tim的mini版本。 登录界面&#xff1a;使用之前做的登录界面&#xff0c;后续修改。原…

java实现即时通讯软件

导读:即时通讯软件即所谓的聊天工具,其主要用途是用于文字信息的传递与文件传输。使用eclipse作为即时通讯软件的开发工具,使用Socket建立通讯渠道,多线程实现多台计算机同时进行信息的传递,swing技术等进行实际开发相对比较合适。通过一些轻松的注册登录后,在局域网中即…

IM即时通讯软件系统源码安卓、苹果、PC端全开源!

demo软件园每日更新资源,请看到最后就能获取你想要的: ​ 1.《计算机系统结构&#xff1a;解析思路习题》课后答案 "本书是按照全国高等教育自学考试指导委员会制定的计算机及应用专业独立本科段“计算机系统结构自学考试大纲’’要求&#xff0c;并以其指定的自学教材内…

即时通讯软件开发:如何解决网络不稳定的问题?

随着智能手机和互联网的普及&#xff0c;即时通讯软件成为人们生活中不可或缺的一部分。随着即时通讯软件的使用越来越普及&#xff0c;网络不稳定的问题也越来越严重。为了提供更好的服务&#xff0c;我们需要解决这个问题。 网络不稳定的原因 网络不稳定可能由许多因素引起…

im即时通讯软件开发:一文即懂什么是高并发

在即时通讯网社区里&#xff0c;多是做IM、消息推送、客服系统、音视频聊天这类实时通信方面的开发者&#xff0c;在涉及到即时通讯技术时聊的最多的话题就是高并发、高吞吐、海量用户。 代码还没开始写&#xff0c;就考虑万一哪天这IM用户量破百万、千万该怎么办的问题&#…