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

容器集合类,是后端的基础,本文以面向对象语言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"); // 根据键获取值
相关推荐
龟四崛起4 分钟前
你的绩效是不是常年都是B
java·经验分享·程序人生·职场和发展·职场发展
⠀One0ne5 分钟前
软件设计原则(Java实现/给出正例反例)
java·软件工程
泥菩萨^_^8 分钟前
【PHP代码审计】PHP基础知识
开发语言·php
ac-er888812 分钟前
数据爬虫中遇到验证码的解决方法
开发语言·爬虫·python
街 三 仔13 分钟前
【C语言零基础入门篇 - 6】:数组、字符和字符串带你探索无限可能
c语言·开发语言
GoppViper31 分钟前
uniapp中实现<text>文本内容点击可复制或拨打电话
前端·后端·前端框架·uni-app·前端开发
拾木20031 分钟前
常见的限流算法
java·开发语言
处处清欢36 分钟前
MaintenanceController
java·开发语言
不染_是非36 分钟前
Django学习实战篇四(适合略有基础的新手小白学习)(从0开发项目)
数据库·后端·学习·django·web
牵牛老人42 分钟前
Qt技巧(三)编辑框嵌入按钮,系统位数判断,判断某对象是否属于某种类,控件取句柄,支持4K,巧用QEventLoop,QWidget的窗体样式
开发语言·qt