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

容器集合类,是后端的基础,本文以面向对象语言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"); // 根据键获取值
相关推荐
SteveKenny15 分钟前
Python 梯度下降法(六):Nadam Optimize
开发语言·python
Hello.Reader1 小时前
深入浅出 Rust 的强大 match 表达式
开发语言·后端·rust
xrgs_shz3 小时前
MATLAB的数据类型和各类数据类型转化示例
开发语言·数据结构·matlab
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
Miketutu5 小时前
Spring MVC消息转换器
java·spring
乔冠宇5 小时前
Java手写简单Merkle树
java·区块链·merkle树
LUCIAZZZ6 小时前
简单的SQL语句的快速复习
java·数据库·sql
来恩10036 小时前
C# 类与对象详解
开发语言·c#
komo莫莫da7 小时前
寒假刷题Day19
java·开发语言