在C#中,Array,List,ArrayList,Dictionary,Hashtable,SortList,Stack的区别

devtools/2025/2/8 1:20:42/

Array

Array你可以理解为是所有数组的大哥

普通数组   :   特点是长度固定, 只能存储相同类型的数据
    static void Main(string[] args){//声明int[] ints;string[] strings;People[] peoples;//默认值 //int 类型是 0//string 类型是 nullint[] ints1 = { 1, 2, 3 };string[] strings1 = { "张三", "李四", "王五" };//数组里面如果存 值类型 存储就是值本身//数组里面如果存 引用类型 存储的就是内存地址//数组遍历for (int i = 0; i < ints1.Length; i++){Console.WriteLine(ints1[i]);}foreach (var s in strings1){Console.WriteLine(s);}int[] ages = { 18, 19, 2, 30, 60, 15, 14 };//Array类上的方法//1.Clear() 将数组恢复成默认值 参1:索引  参2:长度Array.Clear(ints1,1,1);//2.Copy()  复制//Array.Copy(ints1, strings1, ints1.Length);//3.Reverse() 反转Array.Reverse(ints1);//4.IndexOf() 从前往后查询参数2在参数1中首次出现的位置,有则返回索引 没有返回-1//参1:数组  参2:该数在参1出现的位置  参3:指定开始查询位置  参4:查询的个数Array.IndexOf(ages,30);//5.LastIndexOF() 从后向前查找参2数据,出现在参1数组中,有则返回索引,没有返回-1Array.LastIndexOf(ages,30);//6.Find() 从前往后查询参数2在参数1中首次出现的位置 ,有则返回值 没有返回数据默认值Array.Find(ages, x => x > 18);//7.FindLast() 从后往前查询参数2条件的值 有则返回查到的值 没有返回数据类型默认值Array.FindLast(ages, x => x <18);//8.FindIndex() 从前往后查询参数2条件的值 有则返回查到的值的索引 没有返回-1Array.FindIndex(ages, x => x ==18);//9.FindLastIndex() 从后向前查询参数2条件的值 返回的是一个数组Array.FindLastIndex(ages, x => x ==18);//10.FindAll() 查找数组中所有符合参数2条件的值 返回的是一个数组Array.FindAll(ages, x => x % 2 == 0);//11.TrueForAll() 判断数组中的数据是否全部满足参数2,如果满足返回true 只要有一个不满足 则返回falseArray.TrueForAll(ages, x => x>0);//12.Exists()  判断数组中是否有一项满足参数2的条件,只要有一项满足条件 则返回true 所有不满足则返回falseArray.Exists(ages,x=>x%2==0);//实例上的方法://1.CopyTo()//2.GetLength()  获取指定维度长度//3.SetValue()   设置值//4.GetValue()   获取值}
}
class People
{public string Name { get; set; }
}

List

//List: 集合  只能存储相同类型的数据,List的长度是不固定的
//格式: List<数据类型> 变量名 = new List<数据类型>();List<string>list=new List<string>() { "1","2","3"};
List<int> list2=new List<int>(){1,2,3};
list[0] = "1111";
Console.WriteLine(list[0]);
Console.WriteLine(list.Count);list.Sort();
list.Reverse();
list.Clear();
list.IndexOf("1");
list.Insert(0,"2");

ArrayList

  #region ArrayList//ArrayList 是一个动态数组 不固定长度和类型ArrayList list1 = new ArrayList();ArrayList array=new ArrayList() { "你好",1,2,true,new int[] {1,2,3} };//获取动态数组的长度Console.WriteLine(array.Count);array[0] = 100;Console.WriteLine(array[0]);//1.Add 向ArrayList 的最后位置添加数据list1.Add(100);//2.AddRange()int[] ints2 = {1,2,3,4,5,6};list1.AddRange(ints2);ArrayList array2 = new ArrayList() {"Hello Word!" };list1.AddRange(array2);//3.Insert() 在指定索引位置插入数组list1.Insert(1,"小丑");//4.InsertRange() 在指定的索引位置 插入集合的内容list1.InsertRange(2,ints2);//5.Clear()list1.Clear();//6.GetRange() 从集合中截取对应的数据 返回一个新的ArrayList//参1:开始索引的位置//参2:截取的个数ArrayList arr = list1.GetRange(1, 3);//7.Remove() 删除动态数组中指定的第一个值array.Remove(true);//8.RemoveAt() 删除数组中指定索引位置的数据array.RemoveAt(0);//9.RemoveRange() 删除指定范围数据 从索引1的位置开始删除 删除两个array.RemoveRange(1, 2);//10.SetRange() 将参数2集合中的数据 复制到当前动态数组中//参数1:指定从动态数组中 第几个索引开始array.SetRange(0, array2);#endregion

Dictionary

 #region Dictionary//Dictionary(字典) 使用"键"来操作//固定数据类型  长度不固定//键: 标识  在一个字典中  键是唯一的 并且不能为null//格式:  Dictionary<键的数据类型,值的数据类型>变量名=newDictionary<string,int> dic = new Dictionary<string,int>(){{"1",666 },{"2",222 },{"3",444 },{"4",555 }};//向字典中添加数据  参数1:键  参数2:值dic.Add("你好", 666);//取值Console.WriteLine(dic["1"]);//修改dic["2"] = 333;//键值对的个数Console.WriteLine(dic.Count);//判断字典中是否包含指定的key(键)和Value(值)Console.WriteLine(dic.ContainsKey("4"));Console.WriteLine(dic.ContainsValue(666));#endregion

Hashtable

 #region Hashtable//Hashtable  哈希表  表示一系列由键和值组成的数据  使用键访问Hashtable hashtable = new Hashtable(){{1,"1" },{2,"2"},{1,1 },{"2",2 },{true,false},};hashtable.Add("8", "6666");Console.WriteLine(hashtable[1]);hashtable["2"] = "你好";//Keys 获取哈希表中所有的键Console.WriteLine(hashtable.Keys);//Values 获取哈希表中所有的值Console.WriteLine(hashtable.Values);//是否拥有固定大小Console.WriteLine(hashtable.IsFixedSize);//是否只读Console.WriteLine(hashtable.IsReadOnly);#endregion

SortList

  #region SortList 排序列表SortedList sortedList = new SortedList(){{10,"这是10" },{1,"这是1"},{ 2,"这是2"}};sortedList.Add(9, "这是9");//GetByIndex()  通过索引进行访问  排序列表会自动根据键进行排序,索引为0的时候,获取的键值对是 键最小的那个键对值Console.WriteLine(sortedList.GetByIndex(0));sortedList[2] = "这个变20了";Console.WriteLine(sortedList.GetByIndex(1));//GetKey()  通过索引进行访问  获取键值对的 键Console.WriteLine(sortedList.GetKey(2));foreach (int key in sortedList.Keys){Console.WriteLine(key+"\t");}foreach (string key in sortedList.Values){Console.WriteLine(key+"\t");}Console.WriteLine(sortedList.Count);#endregion

Stack

  #region Stack 堆栈Stack<string> stack = new Stack<string>();//添加元素  推入元素stack.Push("张三");stack.Push("李四");stack.Push("王五");Console.WriteLine(stack.Count);//移除并返回在堆栈顶部的对象Console.WriteLine(stack.Pop());//返回在堆栈顶部的对象,但不移除它Console.WriteLine(stack.Peek());Queue<string> queue = new Queue<string>();queue.Enqueue("张三");queue.Enqueue("李四");queue.Enqueue("王五");queue.Dequeue();Console.WriteLine(queue.Peek());#endregion


http://www.ppmy.cn/devtools/156984.html

相关文章

Chapter2 Amplifiers, Source followers Cascodes

Chapter2 Amplifiers, Source followers & Cascodes MOS单管根据输入输出, 可分为CS放大器, source follower和cascode 三种结构. Single-transistor amplifiers 这一章学习模拟电路基本单元-单管放大器 单管运放由Common-Source加上DC电流源组成. Avgm*Rds, gm和rds和…

鼠标拖尾特效

文章目录 鼠标拖尾特效一、引言二、实现原理1、监听鼠标移动事件2、生成拖尾元素3、控制元素生命周期 三、代码实现四、使用示例五、总结 鼠标拖尾特效 一、引言 鼠标拖尾特效是一种非常酷炫的前端交互效果&#xff0c;能够为网页增添独特的视觉体验。它通常通过JavaScript和C…

【数据采集】基于Selenium采集豆瓣电影Top250的详细数据

基于Selenium采集豆瓣电影Top250的详细数据 Selenium官网:https://www.selenium.dev/blog/ 豆瓣电影Top250官网:https://movie.douban.com/top250 写在前面 实验目标:基于Selenium框架采集豆瓣电影Top250的详细数据。 电脑系统:Windows 使用软件:PyCharm、Navicat 技术需求…

C# OpenCV机器视觉:利用TrashNet实现垃圾分类

在繁华的都市里&#xff0c;垃圾分类成了让人头疼的难题。阿强住在一个老旧小区&#xff0c;每天扔垃圾的时候&#xff0c;他都要对着垃圾桶纠结半天&#xff1a;“这到底是可回收物&#xff0c;还是有害垃圾啊&#xff1f;要是分错了&#xff0c;会不会被罚款&#xff1f;” 阿…

MySQL——表操作及查询

一.表操作 MySQL的操作中&#xff0c;一些专用的词无论是大写还是小写都是可以通过的。 1.插入数据 INSERT [INTO] table_name (列名称…)VALUES (列数据…), (列数据…); "[]"表示可有可无&#xff0c;插入时&#xff0c;如果不指定要插入的列&#xff0c;则表示默…

HTTP和HTTPS协议详解

HTTP和HTTPS协议详解 HTTP详解什么是http协议http协议的发展史http0.9http1.0http1.1http2.0 http协议的格式URI和URL请求request响应response http协议完整的请求与响应流程 HTTPS详解为什么使用HTTPSSSL协议HTTPS通信过程TLS协议 HTTP详解 什么是http协议 1、全称Hyper Tex…

iOS 音频录制、播放与格式转换

iOS 音频录制、播放与格式转换:基于 AVFoundation 和 FFmpegKit 的实现 在 iOS 开发中,音频处理是一个非常常见的需求,比如录音、播放音频、音频格式转换等。本文将详细解读一段基于 AVFoundation 和 FFmpegKit 的代码,展示如何实现音频录制、播放以及 PCM 和 AAC 格式之间…

几种K8s运维管理平台对比说明

目录 深入体验**结论**对比分析表格**1. 功能对比****2. 用户界面****3. 多租户支持****4. DevOps支持** 细对比分析1. **Kuboard**2. **xkube**3. **KubeSphere**4. **Dashboard****对比总结** 深入体验 KuboardxkubeKubeSphereDashboard 结论 如果您需要一个功能全面且适合…