您当前的位置: 首页 > 

寒冰屋

暂无认证

  • 0浏览

    0关注

    2286博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

在C#中对列表/数组进行碎片整理——关闭所有空白

寒冰屋 发布时间:2021-01-20 21:13:26 ,浏览量:0

目录

介绍

使用代码

1.列表示例的碎片整理

2.数组示例的碎片整理

更多功能

  • 下载源代码(ZIP)-3.7 KB
  • 下载源代码(7z)-3.2 KB

介绍

1、此方法有助于替换列表的空元素或默认元素。元素的空引用或默认引用将在列表中替换。

public static bool Defragment(IList list, Func isEmptyOrDefaultElement)
       {
           if (list == null || isEmptyOrDefaultElement == null)
           {
               throw new Exception("Check the parameters they can not be null.");
           }

           bool result = false;
           int maxCount = list.Count - 1;

           for (int i = maxCount; i >= 0; i--)
           {
               if (isEmptyOrDefaultElement(list[i]))
               {
                   continue;
               }
               else
               {
                   for (int j = 0; j < i; j++)
                   {
                       if (!isEmptyOrDefaultElement(list[j]))
                       {
                           continue;
                       }
                       else
                       {
                           var value = list[j];
                           list[j] = list[i];
                           list[i] = value;
                           result = true;
                           break;
                       }
                   }
               }
           }

           return result;
       }

2、此方法有助于替换数组的空元素或默认元素。元素的空引用或默认引用将在数组中替换。

public static bool Defragment(T[] array, Func isEmptyOrDefaultElement)
      {
          if (array == null || isEmptyOrDefaultElement == null)
          {
              throw new Exception("Check the parameters they can not be null.");
          }

          bool result = false;
          int maxCount = array.Length - 1;

          for (int i = maxCount; i >= 0; i--)
          {
              if (isEmptyOrDefaultElement(array[i]))
              {
                  continue;
              }
              else
              {
                  for (int j = 0; j < i; j++)
                  {
                      if (!isEmptyOrDefaultElement(array[j]))
                      {
                          continue;
                      }
                      else
                      {
                          var value = array[j];
                          array[j] = array[i];
                          array[i] = value;
                          result = true;
                          break;
                      }
                  }
              }
          }

          return result;
      }
使用代码
1.列表示例的碎片整理
[TestMethod]
      public void ListWithPrimitiveDataTypesCase1_TheListIsDefragmented()
      {
          // prepare
          IList list = new List() { 0, 0, 0, 1, 0, 0, 1, 1 };

          // act
          Defragmentation.Defragment(list, (x) => x == 0);

          // assert
          Assert.AreEqual(1, list[0]);
          Assert.AreEqual(1, list[1]);
          Assert.AreEqual(1, list[2]);
          Assert.AreEqual(0, list[3]);
          Assert.AreEqual(0, list[4]);
          Assert.AreEqual(0, list[5]);
          Assert.AreEqual(0, list[6]);
          Assert.AreEqual(0, list[7]);
      }
2.数组示例的碎片整理
[TestMethod]
      public void ArrayWithPrimitiveDataTypesCase1_TheArrayIsDefragmented()
      {
          // prepare
          int[] array = { 0, 0, 0, 1, 0, 0, 1, 1 };

          // act
          Defragmentation.Defragment(array, (x) => x == 0);

          // assert
          Assert.AreEqual(1, array[0]);
          Assert.AreEqual(1, array[1]);
          Assert.AreEqual(1, array[2]);
          Assert.AreEqual(0, array[3]);
          Assert.AreEqual(0, array[4]);
          Assert.AreEqual(0, array[5]);
          Assert.AreEqual(0, array[6]);
          Assert.AreEqual(0, array[7]);
      }
更多功能

替换元素时,可以将值从数组/列表的非空元素复制到空元素。有关更多功能,请下载本文准备的文件和测试并进行研究。该项目包括其他两种方法:

1、对于列表:

public static bool Defragment(T[] array, Func isEmptyOrDefaultElement,
                                         Action copyFromSourceToDestination)

2、对于数组:

public static bool Defragment(IList list, Func isEmptyOrDefaultElement,
                              Action copyFromSourceToDestination)

https://www.codeproject.com/Tips/5287288/Defragmentation-of-List-Array-in-Csharp-Closing-Al

关注
打赏
1665926880
查看更多评论
立即登录/注册

微信扫码登录

0.0714s