当前位置: 首页 > news >正文

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

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.lryc.cn/news/532806.html

相关文章:

  • 微服务知识——微服务架构的演进过程
  • Chrome 浏览器:互联网时代的浏览利器
  • 深入浅出 NRM:加速你的 npm 包管理之旅
  • Linux——基础命令1
  • nuxt3中使用useFetch请求刷新不返回数据或返回html结构问题解决-完整nuxt3useFetchtch请求封装
  • Kubernetes 中 BGP 与二层网络的较量:究竟孰轻孰重?
  • C中静态库和动态库的使用
  • Debian 安装 Nextcloud 使用 MariaDB 数据库 + Caddy + PHP-FPM
  • 【FPGA】 MIPS 12条整数指令 【3】
  • Mac 部署Ollama + OpenWebUI完全指南
  • 蓝桥杯小白打卡第二天
  • Docker Compose:容器编排的利器
  • springboot项目的单元测试
  • JVM图文入门
  • cursor 开发java项目教程简单上手
  • 优化fm.jiecao.jcvideoplayer_lib中视频横竖屏自动适配原视频方案
  • aws(学习笔记第二十七课) 使用aws API Gateway+lambda体验REST API
  • 物联网的三层架构:感知层、网络层与应用层
  • 常用抓包工具tcpdump、Fiddler、Charles、Wireshark 和 Sniffmaster 下载地址
  • π0开源了且推出自回归版π0-FAST——打造机器人动作专用的高效Tokenizer:比扩散π0的训练速度快5倍但效果相当
  • js-对象-JSON
  • Houdini subuv制作输出阵列图
  • 虚幻基础17:动画蓝图
  • 路由器及工作原理与常用路由配置命令详解
  • Windows编程:下载与安装 Visual Studio 2010
  • R语言 文本分析 天龙八部
  • 深度学习 Pytorch 建模可视化工具TensorBoard的安装与使用
  • 【免费】2007-2019年各省科技支出占一般公共预算支出的比重数据
  • 19爬虫:使用playwright登录超级鹰
  • [转]Java面试近一个月的面试总结