如何制作未知大小的变量? [英] How can I make a variable of unknown size?

查看:83
本文介绍了如何制作未知大小的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将double类型的变量设为:



I want to make a variable of type double as:

double[,] tempData = new double[5000,2];





但我不知道想要将大小定义为5000.

我希望大小(即5000)由从外部源(例如* .csv文件)读取的行数定义。我不想使用这些列表!

有人可以帮忙吗?



问候

AM



but I don't want to define the size as 5000.
I want the size (i.e. 5000) to be defined by the number of lines which are read from external sources (e.g a *.csv file). I don't want to use the Lists!
Can anybody help?

regards
AM

推荐答案

好吧,读取CSV文件,确定有多少行,并分配该数量的数组元素。然后将CSV数据处理到您的数组中。



如果您不想使用像List< T>这样的灵活数组。 (它的核心是一个在展开时复制的数组)然后你需要构建一些描述的链表,或者事先知道要分配多少项。
Well, read the CSV file, identify how many lines there are, and allocate that number of array elements. Then process the CSV data into your array.

If you don't want to use a "flexible" array like List<T> (and it is at heart an array that is copied when it expands) then you either need to construct a linked list of some description, or know in advance how many items to allocate.


In Array.Resize< t>方法 [ ^ ]文档有一个调整二维数组大小的提议。也许这会对你有所帮助。



根据情况,我仍然会考虑去列表(或类似),特别是如果您认为将来需要更复杂的处理,如从中间删除项目,查询操作等...
In Array.Resize<t> Method[^] documentation there's a proposal for resizing two-dimensional arrays. Perhaps that would help you.

Depending on the situation I'd still consider going to lists (or similar) especially if you think you need more complex handling in the future, like removing items from the middle, query operations and so on...


int numLines;
//...set numLines...
double[,] tempData = new double[numLines,2];


这篇关于如何制作未知大小的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆