如何将 CSV 文件读入已初始化的 System.Array 中?
我想创建一个数组并初始化它,例如用于测试:
string[] myList= new string[]
{ "item1",
"item2",
}
如果稍后我想从 csv 文件填充它,我可以向其中添加任意数量的项目吗?
我不想使用动态数组,因为当我必须手动执行时,使用 .add 方法的初始化语法并不那么方便。
I want to create an array and initialize it with for example for testing:
string[] myList= new string[]
{ "item1",
"item2",
}
If later I want to fill it from a csv file, will I be able to add any number of items to it ?
I don't want to use dynamic array because initialization syntax with .add method is not as convenient when I have to do it by hand.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组具有固定大小,因此您无法向其中添加任何数量的数字。您可以做的最接近的事情是创建一个足够大的数组来存储您最有可能生成的数字量,但这是非常低效的,并且如果您生成的数字比您最初预期的多,也容易出错。
您将不得不使用动态数据结构,例如 ArrayList,您可能会觉得它不方便,但它更容易,并且使您的代码看起来更整洁、更高效。
Arrays have a fixed size, so you will not be able to add any amount of numbers to it. The closest thing you can do is to create an array that is large enough to store the amount of numbers you will be most likely generating but this is highly inefficient and also prone to errors if you generate more numbers than you have initially anticipated.
You will have to use a dynamic data structure such as an ArrayList, you might not find it convenient but it is much easier and makes you code look neater and more efficient.
不,您不能添加多个项目。
List< 的问题是什么?字符串>
no , you can not add multiple items.
What is the problem with List< string>