爆款云主机2核4G限时秒杀,88元/年起!
查看详情

活动

天翼云最新优惠活动,涵盖免费试用,产品折扣等,助您降本增效!
热门活动
  • 618智算钜惠季 爆款云主机2核4G限时秒杀,88元/年起!
  • 免费体验DeepSeek,上天翼云息壤 NEW 新老用户均可免费体验2500万Tokens,限时两周
  • 云上钜惠 HOT 爆款云主机全场特惠,更有万元锦鲤券等你来领!
  • 算力套餐 HOT 让算力触手可及
  • 天翼云脑AOne NEW 连接、保护、办公,All-in-One!
  • 中小企业应用上云专场 产品组合下单即享折上9折起,助力企业快速上云
  • 息壤高校钜惠活动 NEW 天翼云息壤杯高校AI大赛,数款产品享受线上订购超值特惠
  • 天翼云电脑专场 HOT 移动办公新选择,爆款4核8G畅享1年3.5折起,快来抢购!
  • 天翼云奖励推广计划 加入成为云推官,推荐新用户注册下单得现金奖励
免费活动
  • 免费试用中心 HOT 多款云产品免费试用,快来开启云上之旅
  • 天翼云用户体验官 NEW 您的洞察,重塑科技边界

智算服务

打造统一的产品能力,实现算网调度、训练推理、技术架构、资源管理一体化智算服务
智算云(DeepSeek专区)
科研助手
  • 算力商城
  • 应用商城
  • 开发机
  • 并行计算
算力互联调度平台
  • 应用市场
  • 算力市场
  • 算力调度推荐
一站式智算服务平台
  • 模型广场
  • 体验中心
  • 服务接入
智算一体机
  • 智算一体机
大模型
  • DeepSeek-R1-昇腾版(671B)
  • DeepSeek-R1-英伟达版(671B)
  • DeepSeek-V3-昇腾版(671B)
  • DeepSeek-R1-Distill-Llama-70B
  • DeepSeek-R1-Distill-Qwen-32B
  • Qwen2-72B-Instruct
  • StableDiffusion-V2.1
  • TeleChat-12B

应用商城

天翼云精选行业优秀合作伙伴及千余款商品,提供一站式云上应用服务
进入甄选商城进入云市场创新解决方案
办公协同
  • WPS云文档
  • 安全邮箱
  • EMM手机管家
  • 智能商业平台
财务管理
  • 工资条
  • 税务风控云
企业应用
  • 翼信息化运维服务
  • 翼视频云归档解决方案
工业能源
  • 智慧工厂_生产流程管理解决方案
  • 智慧工地
建站工具
  • SSL证书
  • 新域名服务
网络工具
  • 翼云加速
灾备迁移
  • 云管家2.0
  • 翼备份
资源管理
  • 全栈混合云敏捷版(软件)
  • 全栈混合云敏捷版(一体机)
行业应用
  • 翼电子教室
  • 翼智慧显示一体化解决方案

合作伙伴

天翼云携手合作伙伴,共创云上生态,合作共赢
天翼云生态合作中心
  • 天翼云生态合作中心
天翼云渠道合作伙伴
  • 天翼云代理渠道合作伙伴
天翼云服务合作伙伴
  • 天翼云集成商交付能力认证
天翼云应用合作伙伴
  • 天翼云云市场合作伙伴
  • 天翼云甄选商城合作伙伴
天翼云技术合作伙伴
  • 天翼云OpenAPI中心
  • 天翼云EasyCoding平台
天翼云培训认证
  • 天翼云学堂
  • 天翼云市场商学院
天翼云合作计划
  • 云汇计划
天翼云东升计划
  • 适配中心
  • 东升计划
  • 适配互认证

开发者

开发者相关功能入口汇聚
技术社区
  • 专栏文章
  • 互动问答
  • 技术视频
资源与工具
  • OpenAPI中心
开放能力
  • EasyCoding敏捷开发平台
培训与认证
  • 天翼云学堂
  • 天翼云认证
魔乐社区
  • 魔乐社区

支持与服务

为您提供全方位支持与服务,全流程技术保障,助您轻松上云,安全无忧
文档与工具
  • 文档中心
  • 新手上云
  • 自助服务
  • OpenAPI中心
定价
  • 价格计算器
  • 定价策略
基础服务
  • 售前咨询
  • 在线支持
  • 在线支持
  • 工单服务
  • 建议与反馈
  • 用户体验官
  • 服务保障
  • 客户公告
  • 会员中心
增值服务
  • 红心服务
  • 首保服务
  • 客户支持计划
  • 专家技术服务
  • 备案管家

了解天翼云

天翼云秉承央企使命,致力于成为数字经济主力军,投身科技强国伟大事业,为用户提供安全、普惠云服务
品牌介绍
  • 关于天翼云
  • 智算云
  • 天翼云4.0
  • 新闻资讯
  • 天翼云APP
基础设施
  • 全球基础设施
  • 信任中心
最佳实践
  • 精选案例
  • 超级探访
  • 云杂志
  • 分析师和白皮书
  • 天翼云·创新直播间
市场活动
  • 2025智能云生态大会
  • 2024智算云生态大会
  • 2023云生态大会
  • 2022云生态大会
  • 天翼云中国行
天翼云
  • 活动
  • 智算服务
  • 产品
  • 解决方案
  • 应用商城
  • 合作伙伴
  • 开发者
  • 支持与服务
  • 了解天翼云
      • 文档
      • 控制中心
      • 备案
      • 管理中心

      C# 静态与动态数组

      首页 知识中心 软件开发 文章详情页

      C# 静态与动态数组

      2023-07-20 06:06:25 阅读次数:425

      数组,泛型,集合

      C#中的数组是由System.Array类衍生出来的引用对象,因此可以使用Array类中的各种方法对数组进行各种操作。

      一维数组:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 定义一维数组
                  int[] IntArray = new int[10] {1,2,3,4,5,6,7,8,9,10};
      
                  // 定义一维字符串数组
                  string[] StrArray = new string[3];
      
                  StrArray[0] = "abc" ;
                  StrArray[1] = "abc";
      
                  Console.ReadKey();
              }
          }
      }
      

      删除元素(一维数组):

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 定义一维数组
                  int[] IntArray = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
      
                  // 遍历数组
                  foreach (int num in IntArray)
                      Console.WriteLine(num);
                  Console.ReadLine();
      
                  // 通过循环删除第三个元素
                  int Del_Num = 2;
                  for (int x = Del_Num; x < IntArray.Length - Del_Num; x++)
                  {
                      IntArray[x] = IntArray[x - 1];
                  }
                  Console.ReadKey();
              }
          }
      }
      

      寻找最大最小值:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 定义一维数组
                  int[] Array = new int[10] { 57, 32, 78, 96, 33, 11, 78, 3, 78, 2 };
      
                  // 声明两个变量用来存储最大值和最小值
                  int min = int.MaxValue;
                  int max = int.MinValue;
                  int sum = 0;
      
                  for (int i = 0; i < Array.Length; i++)
                  {
                      if (Array[i] > max)
                          max = Array[i];
      
                      if (Array[i] < min)
                          min = Array[i];
      
                      sum += Array[i];
                  }
                  Console.WriteLine("最大值: {0} 最小值: {1} 总和: {2} 平均值: {3}", max, min, sum, sum / Array.Length);
                  Console.ReadKey();
              }
          }
      }
      

      数组组合为字符串:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" };
                  string str = null;
      
                  for (int x = 0; x < name.Length - 1; x++)
                      str += name[x] + "|";
      
                  Console.WriteLine(str + name[name.Length - 1]);
      
                  Console.ReadKey();
              }
          }
      }
      

      数组元素反转:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  String[] name = { "老杨", "老苏", "老邹", "老虎", "老牛", "老马" };
                  string tmp;
      
                  for (int x = 0; x < name.Length / 2; x++)
                  {
                      tmp = name[name.Length - 1 - x];
                      name[x] = name[name.Length - 1 - x];
                      name[name.Length - 1 - x] = tmp;
                  }
      
                  for (int x = 0; x < name.Length - 1; x++)
                      Console.Write(name[x] + " |"  );
      
                  Console.ReadKey();
              }
          }
      }
      

      冒泡排序:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      
      namespace ConsoleApplication1
      {
          class Program
          {
              // 执行排序
              static void Sort(int[] Array)
              {
                  for (int x = 0; x < Array.Length - 1; x++)
                  {
                      for (int y = 0; y < Array.Length - 1 - x; y++)
                      {
                          if (Array[y] > Array[y + 1])
                          {
                              int tmp = Array[y];
                              Array[y] = Array[y + 1];
                              Array[y+1] = tmp;
                          }
                      }
                  }
              }
      
              // 输出结果
              static void Display(int[] Array)
              {
                  for (int x = 0; x < Array.Length; x++)
                  {
                      Console.Write(Array[x] + " ");
                  }
              }
      
              static void Main(string[] args)
              {
                  int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
      
                  Sort(MyArray);
                  Display(MyArray);
      
                  // 使用系统提供的方法排序
                  Array.Sort(MyArray);
                  // 执行一次反向排序
                  Array.Reverse(MyArray);
      
                  Console.ReadKey();
              }
          }
      }
      

      直接插入排序:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      
      namespace ConsoleApplication1
      {
          class Program
          {
              // 执行排序
              static void Sort(int[] Array)
              {
                  for (int x = 0; x < Array.Length; x++)
                  {
                      int tmp = Array[x];
                      int y = x;
                      while ((y > 0) && (Array[y - 1] > tmp))
                      {
                          Array[y] = Array[y-1];
                          --y;
                      }
                      Array[y] = tmp;
                  }
              }
      
              static void Main(string[] args)
              {
                  int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
      
                  Sort(MyArray);
      
                  foreach (int x in MyArray)
                      Console.Write(x + " ");
      
                  Console.ReadKey();
              }
          }
      }
      

      选择排序:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      
      namespace ConsoleApplication1
      {
          class Program
          {
              // 执行排序
              static void Sort(int[] Array)
              {
                  int min = 0;
                  for (int x = 0; x < Array.Length; x++)
                  {
                      min = x;
      
                      for (int y = x + 1; y < Array.Length; y++)
                      {
                          if (Array[y] < Array[min])
                              min = y;
                      }
      
                      int tmp = Array[min];
                      Array[min] = Array[x];
                      Array[x] = tmp;
                  }
              }
      
              static void Main(string[] args)
              {
                  int[] MyArray = new int[10] { 57, 32, 4, 96, 33, 11, 78, 3, 78, 2 };
      
                  Sort(MyArray);
      
                  foreach (int x in MyArray)
                      Console.Write(x + " ");
      
                  Console.ReadKey();
              }
          }
      }
      

      定义二维数组

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 定义二维数组
                  int[,] Array = new int[2,3]{{1,2,4},{4,5,6}};
      
                  Console.WriteLine("数组行数为: {0}", Array.Rank);
                  Console.WriteLine("数组列数为: {0}", Array.GetUpperBound(Array.Rank - 1) + 1);
      
                  for (int x = 0; x < Array.Rank;x++ )
                  {
                      string str = "";
                      for(int y=0;y< Array.GetUpperBound(Array.Rank-1)+1;y++)
                      {
                          str = str + Convert.ToString(Array[x, y]) + " ";
                      }
                      Console.Write(str);
                  }
                  Console.ReadKey();
              }
          }
      }
      

      定义动态二维数组:

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  int Row = Convert.ToInt32(Console.ReadLine());
                  int Col = Convert.ToInt32(Console.ReadLine());
      
                  int[,] Array = new int[Row, Col];
      
                  for (int x = 0; x < Row; x++)
                  {
                      for (int y = 0; y < Col; y++)
                      {
                          Console.Write(x + "-->" + y.ToString() + " ");
                      }
                      Console.WriteLine();
                  }
                  Console.ReadKey();
              }
          }
      }
      

      一维数组的合并:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  int[] Array1 = new int[] { 1, 2, 3, 4, 5 };
                  int[] Array2 = new int[] { 6, 7, 8, 9, 10 };
      
                  // 将Array1 与 Array2 合并成 Array3
                  int Count = Array1.Length + Array2.Length;
                  int[] Array3 = new int[Count];
      
                  for (int x = 0; x < Array3.Length; x++)
                  {
                      if (x < Array1.Length)
                          Array3[x] = Array1[x];
                      else
                          Array3[x] = Array2[x - Array1.Length];
                  }
      
                  foreach (int each in Array3)
                      Console.Write(each + "  ");
                  
                  Console.ReadKey();
              }
          }
      }
      

      二维数组的合并:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  int[] Array1 = new int[] { 1, 2, 3, 4, 5 };
                  int[] Array2 = new int[] { 6, 7, 8, 9, 10 };
      
                  // 将两个一维数组,合并到一个二维数组中
                  int[,] Array3 = new int[2, 5];
      
                  // Rank = 二维数组中的2
                  for (int x = 0; x < Array3.Rank; x++)
                  {
                      switch (x)
                      {
                          case 0:
                              {
                                  for (int y = 0; y < Array1.Length; y++)
                                      Array3[x, y] = Array1[y];
                                  break;
                              }
                          case 1:
                              {
                                  for (int z = 0; z < Array2.Length; z++)
                                      Array3[x, z] = Array2[z];
                                  break;
                              }
                      }
                  }
      
                  // 输出二维数组中的数据
                  for (int x = 0; x < Array3.Rank;x++ )
                  {
                      for(int y=0;y<Array3.GetUpperBound(Array3.Rank-1)+1;y++)
                          Console.Write(Array3[x, y] + " ");
                      Console.WriteLine();
                  }
      
                  Console.ReadKey();
              }
          }
      }
      

      二维数组的拆分:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  int[,] Array = new int[2, 3] { { 1, 3, 5 }, { 3, 4, 6 } };
      
                  int[] ArrayOne = new int[3];
                  int[] ArrayTwo = new int[4];
      
                  for (int x = 0; x < 2; x++)
                  {
                      for(int y= 0; y<3; y++)
                      {
                          switch(x)
                          {
                              case 0: ArrayOne[y] = Array[x, y]; break;
                              case 1: ArrayTwo[y] = Array[x, y]; break;
                          }
                      }
                  }
      
                  foreach (int each in ArrayOne)
                      Console.WriteLine(each);
      
                   Console.ReadKey();
              }
          }
      }
      

      ArrayList 类位于System.Collections命名空间下,它可以动态添加和删除元素,可以将该数组类看作扩充了功能的数组。

      动态数组创建:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 动态创建 ArrayList 并初始化10个数据
                  ArrayList List = new ArrayList(10);
                  for (int x = 0; x < 9; x++)
                      List.Add(x);
      
                  Console.WriteLine("可包含元素数量: {0} ", List.Capacity);
                  Console.WriteLine("实际包含数量: {0}", List.Count);
      
                  foreach (int each in List)
                      Console.Write(each + " ");
                  Console.WriteLine();
      
                  // 将普通数组添加到ArrayList中
                  int[] Array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                  ArrayList List1 = new ArrayList(Array);
                  for (int x = 0; x < List1.Count; x++)
                      Console.Write(List1[x] + "  ");
      
                  Console.ReadKey();
              }
          }
      }
      

      增加/插入/删除元素:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Display(ArrayList x)
              {
                  foreach (int each in x)
                      Console.Write(each + "  ");
                  Console.WriteLine();
              }
      
              static void Main(string[] args)
              {
                  // 动态创建 ArrayList
                  ArrayList List = new ArrayList(10);
      
                  // 像数组增加数据
                  List.Add(100);
                  List.Add(200);
                  List.Add(300);
                  List.Add(400);
                  List.Add(500);
                  Display(List);
      
                  // 插入数据
                  List.Insert(1, 1000);
                  List.Insert(2, 2000);
                  Display(List);
      
                  // 移除指定元素
                  List.Remove(1000);
                  Display(List);
      
                  // 根据索引移除元素
                  List.RemoveAt(1);
                  Display(List);
      
                  // 判断集合中是否包含指定元素
                  bool ret = List.Contains(100);
                  Console.WriteLine(ret);
      
                  // 移除一个范围,从下标1开始向后移除3个元素
                  List.RemoveRange(1, 3);
                  Display(List);
      
                  // 清空所有集合
                  List.Clear();
                  Console.ReadKey();
              }
          }
      }
      

      生成随机数存入集合:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  ArrayList list = new ArrayList();
                  // 创建集合,添加数字,求平均值与和,最大值,最小值
                  list.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
                  int sum = 0;
                  int max = (int)list[0];
      
                  for (int x = 0; x < list.Count;x++ )
                  {
                      if((int)list[x] > max)
                          max = (int)list[x];
                      sum += (int)list[x];
                  }
                  Console.WriteLine("最大值: {0} 总和: {1} 平均值: {2}",max,sum,sum/list.Count);
      
                  list.Clear();
                  // 用来生成随机数,并去重后放入list链表中
                  Random rand = new Random();
                  for (int x = 0; x < 10;x++ )
                  {
                      int num = rand.Next(0,10);
                      // 判断集合中是否有这个随机数
                      if (!list.Contains(num))
                          list.Add(num);
                      else
                          x--;
                  }
                  foreach (int each in list)
                      Console.WriteLine(each);
      
                  Console.ReadKey();
              }
          }
      }
      

      增加并遍历数组: 我们可以直接将多个数组放入到ArrayList容器中,进行存储。

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  ArrayList list = new ArrayList();
                  
                  // 直接追加匿名数组
                  list.Add(new int[] { 1, 2, 3, 4, 5 });
                  list.Add(new int[] { 6, 7, 8, 9, 10 });
      
                  // 定义并追加数组
                  int[] ptr = new int[5] { 100, 200, 300, 400, 500 };
                  list.Add(ptr);
      
                  for (int x = 0; x < list.Count;x++ )
                  {
                      if (list[x] is int[])
                      {
                          for(int y=0; y < ((int[])list[x]).Length; y++)
                          {
                              Console.Write(((int[])list[x])[y] + "   ");
                          }
                          Console.WriteLine();
                      }
                  }
                  Console.ReadKey();
              }
          }
      }
      

      增加遍历结构体:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              public struct Student
              {
                  public int u_id;
                  public string u_name;
                  public int u_age;
      
                  public Student(int id, string name, int age)
                  {
                      this.u_id = id;
                      this.u_name = name;
                      this.u_age = age;
                  }
              }
      
              static void Main(string[] args)
              {
                  ArrayList list = new ArrayList();
      
                  // 定义三个结构
                  Student stu1 = new Student(1001,"admin",22);
                  Student stu2 = new Student(1002, "guest", 33);
                  Student stu3 = new Student(1003, "lyshark", 19);
      
                  // 将结构追加到链表
                  list.Add(stu1);
                  list.Add(stu2);
                  list.Add(stu3);
      
                  // 遍历结构体
                  for (int x = 0; x < list.Count;x++ )
                  {
                      if (list[x] is Student)
                      {
                          Student ptr = (Student)list[x];
                          Console.WriteLine("ID: {0} 姓名: {1} 年龄: {2}", ptr.u_id, ptr.u_name, ptr.u_age);
                      }
                  }
      
                 Console.ReadKey();
              }
          }
      }
      

      队列的使用:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Queue queue = new Queue();
      
                  // 入队
                  for (int x = 0; x < 10;x++ )
                  {
                      queue.Enqueue(x);
                      Console.WriteLine("{0} 入队 -> 队列计数: {1}", x,queue.Count);
                  }
                  // 遍历队列
                  foreach(int each in queue)
                  {
                      Console.WriteLine("队列开始: {0} --> 队列元素: {1}", queue.Peek().ToString(),each);
                  }
                  // 弹出队列
                  while(queue.Count !=0)
                  {
                      int value = (int)queue.Dequeue();
                      Console.WriteLine("{0} 出队列.", value);
                  }
      
                 Console.ReadKey();
              }
          }
      }
      

      栈操作:

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Stack stack = new Stack();
      
                  // 向栈追加数据
                  for (int x = 0; x < 10; x++)
                      stack.Push(x);
      
                  // 查询栈
                  Console.WriteLine("当前栈顶元素为:{0}", stack.Peek().ToString());
                  Console.WriteLine("移出栈顶元素:{0}", stack.Pop().ToString());
                  Console.WriteLine("当前栈顶元素为:{0}", stack.Peek().ToString());
      
                  // 遍历栈
                  foreach (int each in stack)
                      Console.WriteLine(each);
      
                  // 出栈
                  while(stack.Count !=0)
                  {
                      int pop = (int)stack.Pop();
                      Console.WriteLine("{0} 出栈", pop);
                  }
      
                  Console.ReadKey();
              }
          }
      }
      

      hash表的使用 Hashtable 哈希表,他表示键值对的一个集合,这些键值对根据键的哈希代码进行组织,键不可以为空,值可以为空。

      using System;
      using System.Collections;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Hashtable hash = new Hashtable();
      
                  // 添加键值对 key = value
                  hash.Add("id", 1001);
                  hash.Add("name", "lyshark");
                  hash.Add("sex", "男");
                  Console.WriteLine("hash 元素个数: {0}", hash.Count);
      
                  // 移除一个hash值
                  hash.Remove("sex");
      
                  // 根据hash查找 是否存在
                  Console.WriteLine("根据key查找: {0}", hash.Contains("name"));
                  Console.WriteLine("根据key查找: {0}", hash.ContainsValue("lyshark"));
      
                  // 遍历hash表
                  foreach (DictionaryEntry each in hash)
                      Console.WriteLine(each.Key + "\t" + each.Value);
                  Console.WriteLine();
      
                  // 清空hash表
                  hash.Clear();
      
                  Console.ReadKey();
              }
          }
      }
      

      有序哈希表 SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问。

      using System;
      using System.Collections;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  SortedList student = new SortedList();
      
                  // 向序列追加集合
                  student.Add("1001", "Lucy");
                  student.Add("1002", "Lily");
                  student.Add("1003", "Tom");
      
                  // 先判断是否存在某个值然后咋追加
                  if (!student.ContainsValue("LyShark"))
                      student.Add("1004", "LyShark");
      
                  // 遍历学生数据
                  foreach(DictionaryEntry each in student)
                  {
                      string id = each.Key.ToString();
                      string name = each.Value.ToString();
                      Console.WriteLine("ID: {0} 姓名: {1}", id, name);
                  }
      
                  // 删除一个数据
                  student.Remove("1001");
      
                  // 获取键的集合
                  ICollection key = student.Keys;
                  foreach(string each in key)
                  {
                      Console.WriteLine(each + "--> " + student[each]);
                  }
      
                  Console.ReadKey();
              }
          }
      }
      

      泛型类型集合: 效率更高更快,不发生装箱,拆箱等。

      using System;
      using System.Linq;
      using System.Collections;
      using System.Collections.Generic;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  // 创建泛型集合对象
                  List<int> list = new List<int>();
                  list.Add(1);
                  list.Add(2);
                  list.Add(3);
      
                  list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });
                  list.AddRange(list);
      
                  // List泛型集合可以转换为数组
                  int[] array = list.ToArray();
                  Console.WriteLine("数组成员数: {0}", array.Length);
      
      
                  // 字符数组转换为泛型集合
                  char[] chs = new char[] { 'a', 'b', 'c' };
                  List<char> list_char = chs.ToList();
                  Console.WriteLine("字符数组成员数: {0}",list_char.Count);
      
                  Console.ReadKey();
              }
          }
      }
      

      k-v泛型集合: 使用队组,实现的泛型集合。

      using System;
      using System.Linq;
      using System.Collections;
      using System.Collections.Generic;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Dictionary<int, string> dict = new Dictionary<int, string>();
      
                  dict.Add(1, "张三");
                  dict.Add(2, "李四");
                  dict.Add(3, "王五");
      
                  foreach(KeyValuePair<int,string> each in dict)
                  {
                      Console.WriteLine("序号:{0} 数值:{1}", each.Key, each.Value);
                  }
      
                  foreach(var each in dict.Keys)
                  {
                      Console.WriteLine("序号:{0} 数值:{1}", each, dict[each]);
                  }
      
                  Console.ReadKey();
              }
          }
      }
      

      k-v泛型集合: 统计指定的一个字符串中单词的出现频率。

      using System;
      using System.Linq;
      using System.Collections;
      using System.Collections.Generic;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  String str = "welcome to china";
                  // 对组统计出每个字符串出现的次数
                  Dictionary<char, int> dict = new Dictionary<char, int>();
                  for (int x = 0; x < str.Length;x++ )
                  {
                      if (str[x] == ' ')
                          continue;
                      //如果dic已经包含了当前循环到的这个键
                      if (dict.ContainsKey(str[x]))
                          dict[str[x]]++;
                      // 这个字符在集合当中是第一次出现
                      else
                          dict[str[x]] = 1;
                  }
      
                  // 遍历出数量
                  foreach(KeyValuePair<char,int> each in dict)
                  {
                      Console.WriteLine("字母: {0} 出现了: {1} 次", each.Key, each.Value);
                  }
      
                  Console.ReadKey();
              }
          }
      }
      
      版权声明:本文内容来自第三方投稿或授权转载,原文地址:https://my.oschina.net/lyshark/blog/5550863,作者:LyShark,版权归原作者所有。本网站转在其作品的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如因作品内容、版权等问题需要同本网站联系,请发邮件至ctyunbbs@chinatelecom.cn沟通。

      上一篇:C/C++ 结构体与指针笔记

      下一篇:VMware ESX Server 4(vSpere)测试记录

      相关文章

      2025-05-19 09:04:14

      复杂度的OJ练习

      复杂度的OJ练习

      2025-05-19 09:04:14
      代码 , 复杂度 , 思路 , 数组 , 算法
      2025-05-16 09:15:24

      Redis Set集合

      Redis Set集合

      2025-05-16 09:15:24
      set , 个数 , 元素 , 示例 , 集合
      2025-05-16 09:15:24

      如何将一串数字用函数的方法倒过来(C语言)

      如何将一串数字用函数的方法倒过来(C语言)

      2025-05-16 09:15:24
      函数 , 数字 , 数组
      2025-05-16 09:15:24

      jQuery遍历对象、数组、集合

      jQuery遍历对象、数组、集合

      2025-05-16 09:15:24
      jQuery , 对象 , 数组 , 遍历 , 集合
      2025-05-16 09:15:17

      递归,搜索,回溯算法(3)之穷举,暴搜,深搜,回溯,剪枝

      递归,搜索,回溯算法(3)之穷举,暴搜,深搜,回溯,剪枝

      2025-05-16 09:15:17
      回溯 , 子集 , 数组 , 算法 , 递归
      2025-05-14 10:33:31

      计算机小白的成长历程——数组(1)

      计算机小白的成长历程——数组(1)

      2025-05-14 10:33:31
      strlen , 个数 , 元素 , 内存 , 十六进制 , 地址 , 数组
      2025-05-14 10:33:31

      计算机小白的成长历程——习题演练(函数篇)

      计算机小白的成长历程——习题演练(函数篇)

      2025-05-14 10:33:31
      函数 , 字符串 , 数组 , 知识点 , 编写 , 迭代 , 递归
      2025-05-14 10:33:16

      30天拿下Rust之泛型

      在Rust语言中,泛型是一种强大的工具,它允许我们编写可复用且灵活的代码。通过泛型,我们可以创建适用于多种类型的数据结构和函数,而无需为每种类型都重复编写相同的逻辑。在Rust中,泛型通过指定类型参数来实现,这些类型参数会在编译时被具体类型所替换。

      2025-05-14 10:33:16
      Rust , 参数 , 实例 , 泛型 , 示例 , 类型
      2025-05-14 10:02:48

      typescript 将数组清空

      在TypeScript或JavaScript开发中,数组是用于存储和管理一组数据的基础数据结构。当需要清空一个数组时,有多种方法可以实现,而选择合适的方法不仅影响代码的可读性,还会对性能产生一定的影响。不同场景下,选择适合的清空数组的方法至关重要。

      2025-05-14 10:02:48
      length , pop , 引用 , 数组 , 方法
      2025-05-13 09:50:28

      Java 两个小时以后

      最大正方形在一个由 '0' 和 '1' 组成的二维矩阵内,找到只包含 '1' 的最大正方形,并返回其面积。 

      2025-05-13 09:50:28
      length , matrix , nums , target , 数组
      查看更多
      推荐标签

      作者介绍

      天翼云小翼
      天翼云用户

      文章

      33561

      阅读量

      5249128

      查看更多

      最新文章

      复杂度的OJ练习

      2025-05-19 09:04:14

      如何将一串数字用函数的方法倒过来(C语言)

      2025-05-16 09:15:24

      Redis Set集合

      2025-05-16 09:15:24

      Java 两个小时以后

      2025-05-13 09:50:28

      用go语言,现有一棵无向、无根的树,树中有 n 个节点,按从 0 到 n - 1 编号 给你一个整数 n 和一个长度为 n - 1 的二维整数数组 edges

      2025-05-13 09:49:12

      存在重复元素 II-128. 最长连续序列

      2025-05-12 08:58:16

      查看更多

      热门文章

      Arrays类的使用

      2023-06-08 06:23:00

      Python打乱列表/数组原顺序,新列表/数组中元素随机分布

      2023-04-13 09:36:44

      Python数组列表过滤

      2023-04-17 09:39:09

      Java 程序设计 第2章 Java基本语法 笔记

      2023-02-24 09:13:25

      js面试题-1:检查是否存在重复元素

      2023-02-22 06:11:15

      Python零基础入门-5 数据结构(集合和字典)

      2023-05-11 06:06:25

      查看更多

      热门标签

      java Java python 编程开发 代码 开发语言 算法 线程 Python html 数组 C++ 元素 javascript c++
      查看更多

      相关产品

      弹性云主机

      随时自助获取、弹性伸缩的云服务器资源

      天翼云电脑(公众版)

      便捷、安全、高效的云电脑服务

      对象存储

      高品质、低成本的云上存储服务

      云硬盘

      为云上计算资源提供持久性块存储

      查看更多

      随机文章

      【滑动窗口】【二分查找】C++算法:862和至少为 K 的最短子数组

      shell编程-数组与运算符详解(超详细)

      C语言数组指针指针数组

      用go语言,给定一个从0开始索引的整数数组 nums 、两个正整数 k 和 dist 。

      与时俱进的Java——《写给大忙人看的Java核心技术》读后感

      shell基础(3)之数组的基础操作:定义、取值(某个值、截取)、数组属性(数组长度、元素长度)、操作(连接数组、替换、取消)

      • 7*24小时售后
      • 无忧退款
      • 免费备案
      • 专家服务
      售前咨询热线
      400-810-9889转1
      关注天翼云
      • 旗舰店
      • 天翼云APP
      • 天翼云微信公众号
      服务与支持
      • 备案中心
      • 售前咨询
      • 智能客服
      • 自助服务
      • 工单管理
      • 客户公告
      • 涉诈举报
      账户管理
      • 管理中心
      • 订单管理
      • 余额管理
      • 发票管理
      • 充值汇款
      • 续费管理
      快速入口
      • 天翼云旗舰店
      • 文档中心
      • 最新活动
      • 免费试用
      • 信任中心
      • 天翼云学堂
      云网生态
      • 甄选商城
      • 渠道合作
      • 云市场合作
      了解天翼云
      • 关于天翼云
      • 天翼云APP
      • 服务案例
      • 新闻资讯
      • 联系我们
      热门产品
      • 云电脑
      • 弹性云主机
      • 云电脑政企版
      • 天翼云手机
      • 云数据库
      • 对象存储
      • 云硬盘
      • Web应用防火墙
      • 服务器安全卫士
      • CDN加速
      热门推荐
      • 云服务备份
      • 边缘安全加速平台
      • 全站加速
      • 安全加速
      • 云服务器
      • 云主机
      • 智能边缘云
      • 应用编排服务
      • 微服务引擎
      • 共享流量包
      更多推荐
      • web应用防火墙
      • 密钥管理
      • 等保咨询
      • 安全专区
      • 应用运维管理
      • 云日志服务
      • 文档数据库服务
      • 云搜索服务
      • 数据湖探索
      • 数据仓库服务
      友情链接
      • 中国电信集团
      • 189邮箱
      • 天翼企业云盘
      • 天翼云盘
      ©2025 天翼云科技有限公司版权所有 增值电信业务经营许可证A2.B1.B2-20090001
      公司地址:北京市东城区青龙胡同甲1号、3号2幢2层205-32室
      • 用户协议
      • 隐私政策
      • 个人信息保护
      • 法律声明
      备案 京公网安备11010802043424号 京ICP备 2021034386号