面向对象程序设计之常用集合类

容器集合类,是后端的基础,本文以面向对象语言c#与java为例,了解常用的集合类。

c#

List<T>:

System.Collections.Generic.List<T> 是动态数组的泛型集合类。它提供了类型安全性和高效的元素访问。

cs 复制代码
List<string> list = new List<string>();
list.Add("apple"); // 添加元素
list.Insert(1, "pear"); // 在指定位置插入元素
list.Remove("apple"); // 根据元素值删除
list.RemoveAt(0); // 根据索引删除元素
list[0] = "cherry"; // 修改指定索引的元素值
int index = list.IndexOf("pear"); // 查找元素的索引

LinkedList<T>:

System.Collections.Generic.LinkedList<T> 是双向链表的泛型集合类。它支持在任意位置进行元素的插入和删除操作。

cs 复制代码
LinkedList<string> linkedList = new LinkedList<string>();
linkedList.AddLast("apple"); // 添加元素到末尾
linkedList.AddFirst("pear"); // 添加元素到开头
linkedList.RemoveFirst(); // 移除第一个元素
var node = linkedList.First;
linkedList.AddAfter(node, "cherry"); // 修改指定位置的元素值
bool b = linkedList.Contains("apple"); // 判断是否包含某元素

HashSet<T>:

System.Collections.Generic.HashSet<T> 是存储唯一元素的哈希集合类。它提供了快速的查找和插入操作。

cs 复制代码
HashSet<string> set = new HashSet<string>();
set.Add("apple"); // 添加元素
set.Remove("apple"); // 删除指定元素
bool exists = set.Contains("apple"); // 判断是否包含某元素

Dictionary<TKey, TValue>:

System.Collections.Generic.Dictionary<TKey, TValue> 是泛型键值对的哈希表集合类。它提供了快速的查找性能,用于存储唯一的键。

cs 复制代码
Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("apple", 5); // 添加键值对
dict.Remove("apple"); // 根据键删除键值对
dict["apple"] = 10; // 更新指定键的值
bool exists = dict.ContainsKey("apple"); // 判断是否包含指定键

java

ArrayList:

java.util.ArrayList 是动态数组实现的集合类,可以根据需要自动增长大小。它允许存储任意类型的对象,并支持按索引访问。

java 复制代码
ArrayList<String> list = new ArrayList<>();
list.add("apple"); // 添加元素
list.add(1, "pear"); // 在指定位置插入元素
list.remove("apple"); // 根据元素值删除
list.remove(0); // 根据索引删除元素
list.set(0, "cherry"); // 修改指定索引的元素值
int index = list.indexOf("pear"); // 查找元素的索引

LinkedList:

java.util.LinkedList 是双向链表实现的集合类。它支持在任意位置进行元素的插入和删除操作,但随机访问效率较低。

java 复制代码
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("apple"); // 添加元素到末尾
linkedList.addFirst("pear"); // 添加元素到开头
linkedList.remove(); // 移除并返回第一个元素
linkedList.removeFirst(); // 移除第一个元素
linkedList.set(0, "cherry"); // 修改指定索引的元素值
boolean b = linkedList.contains("apple");

HashSet:

java.util.HashSet 是基于哈希表实现的集合类,用于存储唯一的元素。它不保证元素的顺序。

java 复制代码
HashSet<String> set = new HashSet<>();
set.add("apple"); // 添加元素
set.remove("apple"); // 删除指定元素
boolean exists = set.contains("apple"); // 判断是否包含某元素

HashMap:

java.util.HashMap 是基于哈希表实现的键值对集合类。它提供了快速的查找性能,不保证元素的顺序。

java 复制代码
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 5); // 添加键值对
map.remove("apple"); // 根据键删除键值对
map.put("apple", 10); // 更新指定键的值
int value = map.get("apple"); // 根据键获取值
相关推荐
Livingbody1 分钟前
Transformers Pipeline 加载whisper模型实现语音识别ASR
后端
网安INF3 分钟前
RSA加密算法:非对称密码学的基石
java·开发语言·密码学
明月*清风3 分钟前
c++ —— 内存管理
开发语言·c++
蔡蓝8 分钟前
设计模式-观察着模式
java·开发语言·设计模式
HyggeBest13 分钟前
Mysql 宏观架构
mysql·架构
DemonAvenger19 分钟前
Go GOGC环境变量调优与实战案例
性能优化·架构·go
大咖分享课20 分钟前
容器安全最佳实践:云原生环境下的零信任架构实施
安全·云原生·架构
异常君21 分钟前
@Bean 在@Configuration 中和普通类中的本质区别
java·spring·面试
jackson凌22 分钟前
【Java学习笔记】Math方法
java·笔记·学习
WindSearcher23 分钟前
大模型微调相关知识
后端·算法