美文网首页
C# 数组操作

C# 数组操作

作者: 若能遇见 | 来源:发表于2020-12-12 14:29 被阅读0次

动态调整数组大小

Resize
实际上是新数组

 // 创建数组
    double[] arr = { 0.001d, 0.000025d, 0.3135d };
    Console.WriteLine($"原数组大小为 {arr.Length},元素为 {string.Join(",", arr)}。");
    // 将大小调整为 5
    Array.Resize(ref arr, 5);
    Console.WriteLine($"新数组大小为 {arr.Length},元素为 {string.Join(",", arr)}");

    Console.Read();

结果:

    原数组大小为 3,元素为 0.001,2.5E-05,0.3135。
    新数组大小为 5,元素为 0.001,2.5E-05,0.3135,0,0

反转数组

Reverse

    Console.WriteLine("-------- 完全反转 --------");
    string[] a = { "ab", "cd", "ef", "gh" };
    Console.WriteLine("原数组:{0}", string.Join(',', a));
    Array.Reverse(a);
    Console.WriteLine("反转后的数组:{0}\n", string.Join(',', a));

    Console.WriteLine("-------- 部分反转 --------");
    int[] b = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
    Console.WriteLine("原数组:{0}", string.Join(',', b));
    Array.Reverse(b, 3, 4);
    Console.WriteLine("反转后的数组:{0}", string.Join(',', b));

    Console.Read();

结果:

    -------- 完全反转 --------
    原数组:ab,cd,ef,gh
    反转后的数组:gh,ef,cd,ab

    -------- 部分反转 --------
    原数组:0,1,2,3,4,5,6,7,8
    反转后的数组:0,1,2,6,5,4,3,7,8

查找符合条件的元素

Find 查找单个元素
FindAll 查找多个元素

   WorkItem[] items =
   {
       new WorkItem
       {
           ID = 1,
           Title = "工序 1",
           StartTime = new DateTime(2018,7,1,8,36,0),
           EndTime = new DateTime(2018,7,2,17,30,0)
       },
       new WorkItem
       {
           ID = 2,
           Title = "工序 2",
           StartTime = new DateTime(2018, 7, 2, 10, 15, 0),
           EndTime = new DateTime(2018, 7, 5, 18, 0, 0)
       }
    };
     // 查找单个元素
     WorkItem res = Array.Find(items, i =>
     {
         DateTime condition = new DateTime(2018, 7, 15);
         if (i.StartTime > condition)
             return true;
         return false;
     });
     // 查找多个元素
     WorkItem[] resitems = Array.FindAll(items, i =>
     {
         DateTime condition = new DateTime(2018, 7, 10);
         if (i.StartTime > condition)
             return true;
         return false;
     });

查找符合条件的元素索引

FindIndex 返回条件的第一个元素索引。
FindLastIndex 返回符合条件的最后一个元素索引。

     int index = Array.FindIndex(src, i =>
     {
         if (i.Contains("a"))
             return true;
         return false;
     });
     Console.WriteLine("找到包含字母“a”的首个元素的索引:{0}", index);

确定数组元素存在

Exists

   bool res = Array.Exists(stus, x => x.City == "西安");

复制数组中元素

Copy

    int[] arr1 = { 1, 2, 3, 4, 5, 6 };
    int[] arr2 = new int[3];
    // 从arr1复制4、5、6到arr2数组。
    // ①源数组;
    // ②源数组元素开始复制索引;
    // ③目标数组;
    // ④写入元素的索引
    // ⑤要复制的元素个数
    Array.Copy(arr1, 3, arr2, 0, 3);

相关文章

  • ToLua的Example示例学习笔记08_AccessingA

    展示了Lua对C#中的数组对象的几种基本访问方法。 「1」代码 操作代码如下: c#代码如下: 「2」需要了解的部...

  • C# 数组操作 List<>

    简单的介绍一种数组操作的方法。 初始化和增加元素 删除元素 更多方法自行测试一下吧:

  • 数组笔记

    #数组 一.C#中的数组 (一)数组的存储 在C...

  • C#:树型数组,分级模糊查询

    C#:树型数组,分级模糊查询

  • C# EXCEL表的写入操作

    C#操作Excel表的操作实例

  • 10.31学习总结

    今天代课老师讲了c#中的数组。 一维数组:声明数组,分配空间,元素赋值,引用数组元素。 多维数组(声明多维数组时,...

  • 判断为空

    C# 字符串为空 数组为空

  • C#——数组

    关于数组,在C#中我们可以创建一维数组,多维数组以及交错数组。一维数组和多维数组都好理解,交错数组是个什么鬼?其实...

  • c#基础(7) 一维数组 数组基本操作

    数组:相同数据类型的成员组成的一组数据; 方便管理 ;一个数组里只能存放一个类型的值; 注:数组必须进行初始化赋值...

  • 31号c#总结

    31号 在c#的基础上学习数组,从五个方面学习数组,数组的概念,一维数组,多维数组,交错数组,Arry类的使用。数...

网友评论

      本文标题:C# 数组操作

      本文链接:https://www.haomeiwen.com/subject/hdxpgktx.html