C#基础总结:常用的数据结构

server/2025/2/26 21:17:16/

Array:

需要处理的元素数量确定并且需要使用下标时可以考虑,不过建议使用List
数组的内容都是相同类型
数组可以直接通过下标访问
创建时需要固定数组大小

int size = 5; 
int [] test =  new int [size]; string [] test2 =  new string [3]; 
//赋值 
test2[0] =  "chen" ; 
test2[1] =  "j"  ; 
test2[2] =  "d" ; 
//修改 
test2[0] =  "chenjd" ; //排序
int[] myArray = { 4, 3, 5, 1, 2 };
Array.Sort(myArray);//获取索引
int value = Array.IndexOf(myArray, 5);//Array.Copy(sourceArray, destinationArray, length): 将一个数组的元素复制到另一个数组中。
int[] myArray = { 4, 3, 5, 1, 2 };
int[] newArray = new int[6];
//Array.Copy(sourceArray, destinationArray, length)
//length是从原数组复制的长度
Array.Copy(myArray, newArray, 4);

ArrayList:

不必在声明ArrayList时指定它的长度
ArrayList可以存储不同类型的元素
ArrayList不是类型安全的
引入时,泛型还未出现,后泛型集合类(如 List)因其类型安全性和性能优势而变得流行

常用方法:

//new
ArrayList arrayList = new ArrayList();//Add
arrayList.Add("Hello");
arrayList.Add(123);//使用 Insert 方法在特定位置插入一个对象:
arrayList.Insert(0, "First");//移除元素 - 使用 Remove 方法移除一个对象:
arrayList.Remove("Hello");
arrayList.RemoveAt(4); //修改数据 
arrayList[4] = 26; //获取元素索引 - 使用 IndexOf 方法获取对象在 ArrayList 中的索引:
int index = arrayList.IndexOf(123);

List泛型List:

需要处理的元素数量不确定时 通常建议使用
在声明List集合时,我们同时需要为其声明List集合内数据的对象类型,这点又和Array很相似,其实List内部使用了Array来实现
融合了Array可以快速访问的优点以及ArrayList长度可以灵活变化的优点

特点:
1.顺序存储,改查快,增(插入)删慢;
使用 Add 将新元素添加在末尾是很快的,但是使用 Insert 将新元素添加在其他位置就不同了。因为这样会导致后面元素的移动位置(后移),删除同样道理,会导致后面元素的位置前移。这样效率是很低的!
2.长度可变,容量不够会自动扩容;
每次扩容,容量增加一倍。
3.需要指定数据类型,保证类型安全

常用属性:

//Capacity:容量
Console.WriteLine("intList 的当前容量为:{0}", intList.Capacity);  // intList 的当前容量为:4//Count:获取 List<T> 中包含的元素数量。
Console.WriteLine("intList 中的元素数量为:{0}", intList.Count);  // intList 中的元素数量为:3

常用方法:

//1.New - 构造List<string > test4 =  new List< string >();   //2.Add:新增数据   
test4.Add(“Fanyoy”);   
test4.Add(“Chenjd”);   //List<T>是无所谓初始长度的,可以用Add()方法往里面添加元素//3.Insert: 插入数据
List<int> intList = new List<int> {1, 2, 3};
intList.Insert(1, 666);
intList.Insert(2, 888);//[]:[]的实现,直接使用了数组的索引方式获取元素。
//修改数据   
test4[1] = “murongxiaopifu”;   //移除数据   
test4.Clear();	//移除所有元素List<int> intList = new List<int> {1, 1, 2};
intList.RemoveAt(1); 	//移除指定下标元素
intList.Remove(1);		//RemoveAll:移除与指定的谓词所定义的条件相匹配的所有元素。
List<int> intList = new List<int> {1, 2, 3};
intList.RemoveAll(x => x > 1);  // 删除所有大于1的元素//Contains:确定某元素是否在 List<T> 中。
Console.WriteLine(strList.Contains(666) ? "元素存在" : "元素不存在");  // 元素存在//Find:搜索与指定谓词所定义的条件相匹配的元素,并返回整个 List<T> 中的第一个匹配元素
List<int> intList = new List<int> {1, 2, 3};
int ret = intList.Find(x => x > 2);
Console.WriteLine(ret);  // 3//IndexOf:返回 List<T> 或它的一部分中某个值的第一个匹配项的从零开始的索引
List<string> strList = new List<string> {"a", "b", "c"};
Console.WriteLine(strList[1]);  // b
Console.WriteLine(strList.IndexOf("b"));  // 1  //Reverse:将 List<T> 或它的一部分中元素的顺序反转。
List<int> intList = new List<int> {1, 2, 3};
intList.Reverse();//Sort:排序
List<int> intList = new List<int> {4, 5, 1, 3, 2};
intList.Sort();
foreach(var element in intList)
{Console.Write(element + " ");
}
// 1 2 3 4 5

Dictionary<K,T>:

需要键值对,快速操作
字典的实现方式就是哈希表的实现方式,只不过 字典是类型安全的 ,也就是说当创建字典时,必须声明key和item的类型
键值对中的键和值都可以是任何类型的(泛型),但是键必须唯一且不能为 null,而值可以不唯一;
增删改查速度快,查找一个值的时间复杂度接近 O(1);
长度不固定,动态扩容;
比较消耗内存(以空间换时间);

Dictionary<int, string> testDict = new Dictionary<int, string>();
testDict.Add(1, "a");
testDict.Add(2, "b");
testDict.Add(3, "c");
foreach (KeyValuePair<int, string> element in testDict)
{Console.WriteLine("{0} {1}", element.Key, element.Value);
}

常用属性:

//Count:获取包含在 Dictionary<TKey,TValue> 中的键/值对的数目。
Console.WriteLine("testDict 中元素(键值对)的数量为:{0}", testDict.Count);//Keys:获得一个包含 Dictionary<TKey,TValue> 中的键的集合。
foreach (object key in testDict.Keys)
{Console.Write(key + " ");
}
// 1 2 3//Values:获得一个包含 Dictionary<TKey,TValue> 中的值的集合。
foreach (object value in testDict.Values)
{Console.Write(value + " ");
}
// a b c

常用方法:

//add
testDict.Add(2, "b");//Clear:清空字典
testDict.Clear();//ContainsKey:确定是否 Dictionary<TKey,TValue> 包含指定键。
Console.WriteLine(testDict.ContainsKey("王五"));  // True
Console.WriteLine(testDict.ContainsKey("赵六"));  // False//ContainsValue:确定 Dictionary<TKey,TValue> 是否包含特定值。
Console.WriteLine(testDict.ContainsValue(99));  // True
Console.WriteLine(testDict.ContainsValue(100));  // False//Remove:将带有指定键的元素(键值对)从 Dictionary<TKey,TValue> 中移除。
testDict.Remove("sex");//TryGetValue
string value = "";
bool ret = testDict.TryGetValue("name", out value);  // 存在:true 不存在:false
Console.WriteLine("以 name 为键的键值对 {0},对应的值为:{1}", ret ? "存在" : "不存在", value);

区别:
List只是一组项目,Dictionary是一组键值对


项目使用场景:

字典跟枚举一起使用
字典:
public Dictionary<SENSOR_POWER, double> StandbyCurrent = new Dictionary<SENSOR_POWER, double>();

private static Dictionary<string, DTController> _dtControllers = new Dictionary<string, DTController>();

设置枚举结构数据的数组:
public enum CURRENT_RANGE
{
CURRENT_RANGE_MA = 0, ///<电流测试量程为mA
CURRENT_RANGE_UA, ///<电流测试量程为uA
CURRENT_RANGE_NA ///<电流测试量程为nA
};

CURRENT_RANGE[] currentRange = new CURRENT_RANGE[7];

static可作为流程间的交互方法来使用


http://www.ppmy.cn/server/170845.html

相关文章

2024大模型技术全景解构:从开源生态到商业落地的深度博弈

大模型技术调研报告 一、开源大模型概览 模型名称开发公司/机构技术架构参数规模开源协议特点与应用场景LLaMA 3Meta (Facebook)Transformer (Decoder-only)8B、70B、400B需申请商用许可支持多语言&#xff0c;长上下文&#xff08;128K token&#xff09;&#xff0c;优化推…

吃一堑长一智

工作中经历&#xff0c;有感触记录下 故事一 以前在一家公司时&#xff0c;自己是一名开发人员&#xff0c;遇到问题请教领导解决方案&#xff0c;当时领导给了建议&#xff0c;后来上线后出问题了&#xff0c;背了锅。心里想的是领导说这样做的呀&#xff0c;为什么出问题还…

23贪心算法

分发饼干 class Solution { public:int findContentChildren(vector<int>& g, vector<int>& s) {int i0,j0;int count0;sort(s.begin(),s.end());sort(g.begin(),g.end());while(i<g.size()&&j<s.size()){if(g[i]<s[j]){i;j;count;}else…

android中的CheckBox改变背景图片显示大小

在androidStudio的xml文件设置布局时&#xff0c;对于checkBox选中后&#xff0c;展示大小不同的背景图片 1.首先需要一个选择器存放背景图片&#xff0c;设置选中和未选中状态 <?xml version"1.0" encoding"utf-8"?> <selector xmlns:androi…

深入理解 Redis 设计与集群管理

redis 作为一款高性能的键值存储系统&#xff0c;广泛应用于缓存、消息队列和实时分析等场景。随着业务需求的增长&#xff0c;如何有效地管理和扩展 Redis 实例成为了开发者关注的重点。本文将围绕 Redis 的核心设计思想、常见问题及其解决方案展开讨论&#xff0c;并深入介绍…

JMeter性能问题

性能测试中TPS上不去的几种原因 性能测试中TPS上不去的几种原因_tps一直上不去-CSDN博客 网络带宽 连接池 垃圾回收机制 压测脚本 通信连接机制 数据库配置 硬件资源 压测机 业务逻辑 系统架构 CPU过高什么原因 性能问题分析-CPU偏高 - 西瓜汁拌面 - 博客园 US C…

让Word插上AI的翅膀:如何把DeepSeek装进Word

在日常办公中&#xff0c;微软的Word无疑是我们最常用的文字处理工具。无论是撰写报告、编辑文档&#xff0c;还是整理笔记&#xff0c;Word都能胜任。然而&#xff0c;随着AI技术的飞速发展&#xff0c;尤其是DeepSeek的出现&#xff0c;我们的文字编辑方式正在发生革命性的变…

【C语言】指针笔试题

前言&#xff1a;上期我们介绍了sizeof与strlen的辨析以及sizeof&#xff0c;strlen相关的一些笔试题&#xff0c;这期我们主要来讲指针运算相关的一些笔试题&#xff0c;以此来巩固我们之前所学的指针运算&#xff01; 文章目录 一&#xff0c;指针笔试题1&#xff0c;题目一…