【问题标题】:C# increase length of a string array [duplicate]C#增加字符串数组的长度[重复]
【发布时间】:2012-08-03 11:23:04
【问题描述】:

可能重复:
how to inset a new array to my jagged array

我有一个问题,我不知道如何在数组长度中创建一个字符串数组变量。

我现在下面有这段代码:

string[] p = new string[10];
int num = 0;

foreach (Product products in GetAllProducts())
    {
     //do something
     p[num]= "some variable result"
     num++
    }

问题是,我不知道我会得到多少个“p”,尽管我知道它至少会少于 10 个。 但是如果我把它设置为0,当我启动它时我会得到一个错误,因为它不知道“p [num]” 所以我正在寻找使“p”具有可变长度的方法。

有人可以帮帮我吗?谢谢

============已解决===========

List<string> p = new List<string>();
int num = 0;

foreach (Product products in GetAllProducts())
    {
     string s= null;
     //do something ( create s out of multiple parts += s etc.)
     p.add(s)
     num++
    }

感谢解决方案海报

【问题讨论】:

    标签: c# arrays


    【解决方案1】:

    如果您不知道需要添加的项目数,请使用List&lt;string&gt; 而不是数组。

    【讨论】:

    • 啊哈列表。还不知道那个(我知道我知道,我不是专业人士)谢谢这个!
    【解决方案2】:

    您的数组长度在实例化后无法修改。使用ArrayListGeneric Lists

    【讨论】:

      【解决方案3】:
      var p = new new List<string>(10);
      foreach (Product products in GetAllProducts()) 
      { 
          //do something 
          p.Add("some variable result"); 
      } 
      

      【讨论】:

        【解决方案4】:

        GetAllProducts() 返回什么?它有计数或长度吗?你应该先调用它,将它保存在一个变量中,获取计数/长度,然后声明你的数组!

        【讨论】:

          【解决方案5】:

          有两种解决方案。

          如果你想继续使用数组:

          int num = 0;
          var list = GetAllProducts();
          string[] p = new string[list.Length]; // Or list.Count if this is a collection
          
          foreach (Product products in list)
          {
              //do something
              p[num] = "some variable result";
              num++;
          }
          

          否则你应该使用这样的列表:

          List<string> p = new List<string>();
          
          foreach (Product products in GetAllProducts())
          {
              //do something
              p.Add("some variable result");
          }
          

          【讨论】:

            【解决方案6】:

            使用 Array.Resize() 方法,它允许调整它的大小(通过 n 个索引)。 在我的示例中,我将在每一步中重新调整 1:

                    string[] array = new string[3]; //create array
                    for (int i = 0; i < 5; i++)
                    {
                        if (array.Length-1 < i) //checking for the available length
                        {
                            Array.Resize(ref array, array.Length + 1); //when to small, create a new index
                        }
                        array[i] = i.ToString(); //add an item to array[index] - of i
                    }
            

            【讨论】:

              【解决方案7】:

              因为您的代码对 GetAllProducts 的结果使用了 foreach,所以 GetAllProducts 必须返回一个 IEnumerable 集合。可能最好的解决方案是将 GetAllProducts 的结果简单地分配给这样的集合。例如,也许它已经返回了一个字符串列表?所以你可以这样做:

              List<string> strings = GetAllProducts();
              

              如果您已经从 GetAllProducts 返回了一个集合,则无需使用 foreach 循环来创建数组。

              或者简单地说:

              var strings = GetAllProducts();
              

              让编译器计算出字符串的类型。

              您可以使用数组执行大多数操作,也可以使用列表执行其他操作(例如将项目添加到列表的末尾)。

              也许您可以发布 GetAllProducts 的签名(尤其是它的返回类型),以便我们更好地为您提供建议?

              【讨论】:

                【解决方案8】:

                我看到很多人给了你正确的答案,那就是使用列表。如果你最后仍然需要一个数组,你可以很容易地将你的列表转换成这样的数组:

                    string[] tempArray = myList.ToArray();
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-11-15
                  • 2021-12-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2023-03-10
                  • 1970-01-01
                  • 2017-08-19
                  相关资源
                  最近更新 更多