数组、数组列表还是列表的列表?

发布于 2024-12-17 23:25:29 字数 280 浏览 0 评论 0 原文

我打算使用一个标准数组,我需要它的行数为 2 列

double[,] members = new double[x, 2];

,并循环遍历我得到的所有结果,然后将它们添加到数组中,但由于结果的数量可以改变,所以我不这样做想要预先定义数组的大小...

members[0, 0] = cost;
members[x, 1] = tax;
x++;

我正在考虑调整数组的大小,但是使用数组列表或列表列表会更容易吗?

I was going to use a standard array, I need it to x number of rows and be 2 columns

double[,] members = new double[x, 2];

and loop through all the results I get back and then add them to the array, but as the number of results can change I don't want to pre-define the size of the array...

members[0, 0] = cost;
members[x, 1] = tax;
x++;

I was looking at resizing the array but would it be easier just using an arraylist or list of lists for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哑剧 2024-12-24 23:25:29

我会有某种类型的清单,其中该类型有成本和税收。

如果空间是溢价,它可能是一个具有两个双精度的结构(顺便说一句,十进制会更合适吗?它几乎总是一个更好的选择),但是如果是这样:使其不可变。

例如:

public struct YourType {
    private readonly decimal cost, tax;
    public decimal Cost { get { return cost; } }
    public decimal Tax { get { return tax; } }
    public YourType(decimal cost, decimal tax) {
        this.cost = cost;
        this.tax = tax;
    }
}

与:

List<YourType> list = new List<YourType>();

I would have a list of some type, where that type has a cost and a tax.

If space is a premium, it could be a struct with two doubles (btw, would decimal be more appropriate? It is almost always is a better choice for money), but if so: make it immutable.

For example:

public struct YourType {
    private readonly decimal cost, tax;
    public decimal Cost { get { return cost; } }
    public decimal Tax { get { return tax; } }
    public YourType(decimal cost, decimal tax) {
        this.cost = cost;
        this.tax = tax;
    }
}

With:

List<YourType> list = new List<YourType>();
若沐 2024-12-24 23:25:29

也许第二维中的项目数是固定的,或者至少是可预测的,因此如果您使用 Microsoft .NET Framework 4.0,则可以使用元组列表:

List<Tuple<double, double>> list = new List<Tuple<int, int>>();

// In your loop...
list.Add(Tuple.Create(cost, tax));

// ... later when retrieving some tuple...
int item0cost = list[0].Item1;
int item0tax = list[0].Item2;

您始终可以实现某种值对象类,其中具有 CostTax 属性,但是使用元组可以像我在代码示例中向您展示的那样简单。

Perhaps the number of items in the second dimension is fixed, or at least, is predictable, so if you're using Microsoft .NET Framework 4.0, you can use a list of tuples:

List<Tuple<double, double>> list = new List<Tuple<int, int>>();

// In your loop...
list.Add(Tuple.Create(cost, tax));

// ... later when retrieving some tuple...
int item0cost = list[0].Item1;
int item0tax = list[0].Item2;

You can always implement some kind of value object class which has Cost and Tax properties, but using tuples you can do it as easy as I shown you in my code sample.

×纯※雪 2024-12-24 23:25:29

您应该使用 List>

You should use a List<List<int>>.

固执像三岁 2024-12-24 23:25:29

使用List

来自 msdn

ArrayList 已被 List 取代,后者速度更快,功能更多。

T 可以是 Listdouble[2]struct 或 2 元组...但正如 Marc 所说,如果嵌套的 List 注定总是有两个项目,那么使用 List> 就有点过度了。

Use a List<T>.

From msdn:

ArrayList has been replaced by List<T> which is faster, and has more features.

T could be a List<double> or a double[2] or a struct or a 2-tuple... but as stated by Marc, using a List<List<double>> is over-doing things if the nested List is destined to always have two items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文