【问题标题】:How can I count the lenght of an array and not include NULL?如何计算数组的长度而不包括 NULL?
【发布时间】:2019-11-20 09:53:08
【问题描述】:

我已经创建了一个这样的数组

string[] directories = new string[15];

然后我想用它做点什么

for (int i = 0; i < directories.Length; i++) {
   //code
}

用户可以根据需要在数组中输入任意数量的目录,但是如果他们不将 14 个元素放入其中,那么数组的其余部分显然会为 NULL,并且 for 循环不会停止直到它达到第 14 个元素。如何使循环停止在数组中的最后一个目录而不计算 NULL?

我已经尝试过了,但它返回以下错误:System.NullReferenceException: 'Object reference not set to an instance of an object.'

for (int i = 0; i < directories .Length; i++) {
   //code

   string directory = directories[i];

   if (directory.Equals(null)){
      return;
   }

   // more code
}

感谢您,对缺乏经验和英语不好感到抱歉。

【问题讨论】:

标签: c# arrays


【解决方案1】:

尝试:。

directories.Count(x => x != null):

如果你总能保证在第一个 null 之后,其他的都为 null,那么下面的代码将代替你代码中的 if 语句,并且比上面的 linq 更高效:

if (directory == null)
    break;

break 关键字可防止 c# 中的任何循环。

如果您真的想在数组中包含一组值,然后是一组空值,您是否考虑过使用列表而不是数组?

【讨论】:

    【解决方案2】:

    如果directory 为空,您将无法调用Equals()

    试试directory == null

    【讨论】:

      【解决方案3】:

      使用 Linq 来检查目录(如果它们是目录并且字符串不是 null)的一种快速简便的方法是这样的:

      namespace FooApp {
      
          using System.IO;
          using System.Linq;
      
          class MyFoo {
      
              public static void Main(string[] args) {
      
                  foreach (var dir in args.Where(a => !string.IsNullOrEmpty(a) && Directory.Exists(a))) {
                      // Valid directory. Do as you please
                  }
      
              }
      
          }
      }
      

      编辑: 如果您不想检查目录是否有效而只想检查字符串是否为非空,则可以将 Directory.Exists() 排除在外。

      【讨论】:

        【解决方案4】:

        如果你可以跳过索引,你可以使用 Linq 的 Where:

        foreach (var dir in directories.Where(x => x != null) 
        {
        
        }
        

        如果您需要项目索引:

        for (int i = 0; i < directories.Length; i++) 
        {
            string directory = directories[i];
            if (directory == null)
            {
                return; // Note that rest of items will be ignored. method will end code outside for loop will not be executed
                //break; // exit loop on first null. code outside for loop will be executed
                //continue; // use continue to skip null item and process rest of items. code outside for loop will be executed
            }
        
            // more code
        }
        

        【讨论】:

          【解决方案5】:

          使用List &lt;string&gt; 代替string[]

          string strDir = "./";
          List<string> directories = new List<string>();
          directories.Add(strDir);
          
          for (int i = 0; i < directories.Length; i++) {
          //code
          }
          

          编辑:当然可以实现空值检查,但我认为没有必要,因为

          用户可以在数组中输入任意数量的目录

          【讨论】:

          • 这不会神奇地检查列表中的null-entries。
          • null-据我了解原始帖子不应该在列表中。该列表仅包含添加的条目(显然是!= null)!
          • 啊,我明白你的意思了。由于列表具有动态大小,它不包含null-elements。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-10
          • 2019-06-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-18
          • 1970-01-01
          相关资源
          最近更新 更多