【问题标题】:Making an array of an unknown size C# [duplicate]制作一个未知大小的数组C# [重复]
【发布时间】:2013-01-30 16:24:51
【问题描述】:

可能重复:
Array of an unknown length in C#

我想创建一个程序,用户可以在其中输入项目,这些项目将存储在一个数组中。当用户对物品的数量感到满意时,如果他得到了每个物品,程序就会询问。

问题是我似乎无法创建一个大小未知的数组。我尝试使用这样的东西:String[] list = new string[]{}; 但是当程序运行到那里时,它会给出一个 IndexOutOfRangeException。

有什么办法可以做到吗?

这是完整的代码:

bool groceryListCheck = true;
        String[] list = new string[]{};
        String item = null;
        String yon = null;
        int itemscount = 0;
        int count = 0;

        while (groceryListCheck)
        {
            Console.WriteLine("What item do you wanna go shop for?");
            item = Console.ReadLine();
            list[count] = item;
            count++;
            Console.WriteLine("Done?");
            yon = Console.ReadLine();
            if (yon == "y")
            {
                groceryListCheck = false;
                itemscount = list.Count();
            }
            else
            {
                groceryListCheck = true;
            }
        }

        for (int x = 0; x < itemscount; x++)
        {
            Console.WriteLine("Did you got the " + list[x] + "?");
            Console.ReadKey();
        }

【问题讨论】:

  • 数组大小必须在初始化时知道​​。他们不能成长。你想要的是List&lt;String&gt;
  • StackOverflow 上有大量的 C# 数组问题——你看过了吗?特别是stackoverflow.com/questions/599369/… ?

标签: c# arrays


【解决方案1】:

使用List 而不是array

List<string> myList = new List<string>();
myList.Add("my list item");

收集完所有项目后,您可以使用foreach 循环遍历集合中的所有项目。

foreach(string listItem in myList)
{
    Console.WriteLine(listItem);
}

【讨论】:

    【解决方案2】:

    List&lt;string&gt; 会更简单、更灵活。

    有很多使用 List here 的示例,它们向您展示了从中提取数据的各种方法。

    【讨论】:

      【解决方案3】:

      您可以使用List&lt;string&gt;,然后,如果您需要一个数组作为结果,您可以调用.ToArray() 方法。

      【讨论】:

        【解决方案4】:

        我发现将变量列表变成列表是可行的。例如:

                bool groceryListCheck = true;
                List<string> list = new List<string>();
                String item = null;
                String yon = null;
                int itemscount = 0;
        
                while (groceryListCheck)
                {
                    Console.WriteLine("What item do you wanna go shop for?");
                    item = Console.ReadLine();
                    list.Add(item);
                    Console.WriteLine("Done?");
                    yon = Console.ReadLine();
                    if (yon == "y")
                    {
                        groceryListCheck = false;
                        itemscount = list.Count();
                    }
                    else
                    {
                        groceryListCheck = true;
                    }
                }
        
                for (int x = 0; x < itemscount; x++)
                {
                    Console.WriteLine("Did you got the " + list[x] + "?");
                    Console.ReadKey();
                }
        

        这是完整的代码,它对我有用。

        【讨论】:

          【解决方案5】:

          我会说,为此您应该使用 Hashtable。您可以根据需要添加到它,并且在创建它时不需要指定大小。有点像一个非常简单的数据库。

          见:http://www.dotnetperls.com/hashtable

          【讨论】:

            猜你喜欢
            • 2014-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-09-14
            • 1970-01-01
            • 1970-01-01
            • 2013-12-18
            • 2020-11-13
            相关资源
            最近更新 更多