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

容器集合类,是后端的基础,本文以面向对象语言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"); // 根据键获取值
相关推荐
毕设源码-朱学姐23 分钟前
【开题答辩全过程】以 办公自动化管理系统为例,包含答辩的问题和答案
java·eclipse
李宥小哥3 小时前
C#基础11-常用类
android·java·c#
C嘎嘎嵌入式开发3 小时前
(2)100天python从入门到拿捏
开发语言·python
Stanford_11063 小时前
如何利用Python进行数据分析与可视化的具体操作指南
开发语言·c++·python·微信小程序·微信公众平台·twitter·微信开放平台
小许学java4 小时前
数据结构-ArrayList与顺序表
java·数据结构·顺序表·arraylist·线性表
Vallelonga4 小时前
Rust 中的数组和数组切片引用
开发语言·rust
Kiri霧4 小时前
Rust模式匹配详解
开发语言·windows·rust
偶尔的鼠标人4 小时前
Avalonia中,使用DataTable类型作为DataGrid的ItemSource 数据源
ui·c#·avalonia
white-persist5 小时前
Python实例方法与Python类的构造方法全解析
开发语言·前端·python·原型模式
千里马-horse5 小时前
Async++ 源码分析8--partitioner.h
开发语言·c++·async++·partitioner