【问题标题】:Populate String Array With For Loop and String Combintion in C#在 C# 中使用 For 循环和字符串组合填充字符串数组
【发布时间】:2012-05-06 04:13:10
【问题描述】:

我有一个定义为 String[] sEmails; 的字符串数组,我试图用 10 个不同(但相似)的字符串(在本例中为电子邮件地址)填充它。

这是我试图用来填充数组的代码。

public void populateEmailArray()
    {
        for (int x = 0; x < 10; x++)
        {
            switch(x)
            {
                case 1:
                    sEmails[x] = sRepStuff + "1" + sGmail; 
                break;
                case 2:
                    sEmails[x] = sRepStuff + "2" + sGmail;
                break;
                case 3:
                    sEmails[x] = sRepStuff + "3" + sGmail;
                break;
                case 4:
                    sEmails[x] = sRepStuff + "4" + sGmail;
                break;
                case 5:
                    sEmails[x] = sRepStuff + "5" + sGmail;
                break;
                case 6:
                    sEmails[x] = sRepStuff + "6" + sGmail;
                break;
                case 7:
                    sEmails[x] = sRepStuff + "7" + sGmail;
                break;
                case 8:
                    sEmails[x] = sRepStuff + "8" + sGmail;
                break;
                case 9:
                    sEmails[x] = sRepStuff + "9" + sGmail;
                break;
            }
        }
    }

我想要变成这样的结果

sEmails['repstuff1@gmail.com','repstuff2@gmail.com','repstuff3@gmail.com'] 等等等等到 repstuff9@gmail.com

但在第一次尝试设置 sEmails[x] 时,它给了我一个错误“NullReferenceException 未处理。对象引用未设置为对象的实例。”

我不知道我在这里做错了什么,因为代码在我的脑海中似乎是正确的。对此的任何帮助将不胜感激。

【问题讨论】:

    标签: c# arrays string for-loop


    【解决方案1】:

    尝试使用

    实例化您的数组
    String[] sEmails = new String[10];
    

    您还可以使该循环更加简洁:

    public void populateEmailArray()
    {
        for (int x = 0; x < 10; x++)
        {
    
            sEmails[x] = sRepStuff + x + sGmail; 
        }
    }
    

    【讨论】:

    • 是的,我最初只有一个没有开关盒的 for 循环,但我得到了错误,并认为这可能是导致它的原因,感谢您的帮助!
    • 如果我不知道数组大小的最大值,那么如何处理呢?
    • @Arnab 如果您想拥有一个动态数组,请尝试使用 ArrayList 之类的东西 - 它的行为很像数组,但会动态扩展。
    • 目前ArrayList 已被弃用。使用ListDictionary 或此类集合...
    【解决方案2】:

    希望你一切顺利。

    今天我会帮你清理你的代码!

    你可以这样做,而不是做一个 switch-case

    for(int i = 0; i < 10 ; i++)
    {
       emails[i]="repstuff"+i+"@gmail.com";
    }
    

    这可以帮助您清除您的编码风格。此外,您是否检查过您是否已实例化/创建了您的 sEmail、repStuff 和 Gmail?

    【讨论】:

      【解决方案3】:

      数组从 0 开始索引,而不是 1,因此您没有给 sEmails[0] 一个值。将所有值下移 1。然后当您访问 sEmails[0] 时,它仍然是 null。您还应该确保您的 sEmails 数组已被实例化:

      sEmails = new String[10];
      

      这应该可行:

      public void populateEmailArray()
      {
             sEmails = new String[10];
              for (int x = 0; x < 10; x++)
              {
                  switch(x)
                  {
                      case 0:
                          sEmails[x] = sRepStuff + "1" + sGmail; 
                      break;
                      case 1:
                          sEmails[x] = sRepStuff + "2" + sGmail;
                      break;
                      case 2:
                          sEmails[x] = sRepStuff + "3" + sGmail;
                      break;
                      case 3:
                          sEmails[x] = sRepStuff + "4" + sGmail;
                      break;
                      case 4:
                          sEmails[x] = sRepStuff + "5" + sGmail;
                      break;
                      case 5:
                          sEmails[x] = sRepStuff + "6" + sGmail;
                      break;
                      case 6:
                          sEmails[x] = sRepStuff + "7" + sGmail;
                      break;
                      case 7:
                          sEmails[x] = sRepStuff + "8" + sGmail;
                      break;
                      case 8:
                          sEmails[x] = sRepStuff + "9" + sGmail;
                      break;
                      case 9:
                          sEmails[x] = sRepStuff + "10" + sGmail;
                      break;
                  }
              }
          }
      

      更好、更简洁的版本是:

      for(int i = 0; i < 10 ; i++)
      {
         sEmails[i]="repstuff"+(i+1)+"@gmail.com";
      }
      

      【讨论】:

      • 但这不会导致空引用异常,我认为更可能的是该数组还没有被实例化。
      • 这仍然是可怕的编码风格。
      • 已更新以包含这种可能性。我在想它在执行类似 sEmails[0].length() 之类的东西时抛出了异常
      • @Kevin 我包含了一个更好的实现。
      【解决方案4】:

      对于您已经接受的解决方案,我会添加一些“香料”以使其更具活力。我不会设置 10 硬编码,而是使用数组的长度。

      public void populateEmailArray()
      {
          int length = sEmails.Length;
      
          for (int x = 0; x < length; x++)
          {
      
              sEmails[x] = sRepStuff + x + sGmail; 
          }
      }
      

      我不相信自己的记忆力,因为我必须在一段时间后返回程序并且必须记住并检查我必须更改的所有点,例如当您的电子邮件数组必须增长到 20 个元素时。

      【讨论】:

      • 您不应该将数组的长度存储在局部变量中。如果将循环变量直接与长度属性进行比较,x86 抖动将删除数组边界检查,但如果将值分配给本地,则不会执行此操作。 (至少 v 2.0 x86 的抖动确实如此;它可能同时发生了变化。)
      猜你喜欢
      • 2022-12-10
      • 2012-05-25
      • 2019-05-27
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2015-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多