gpt4 book ai didi

c# - 制作未知大小的数组 C#

转载 作者:太空狗 更新时间:2023-10-29 19:51:59 27 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
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();
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com