C# 定义二维动态数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1939319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:02:00  来源:igfitidea点击:

Defining two dimensional Dynamic Array

c#.netarrays

提问by Harikrishna

How can I define two dimensional dynamic array? If I want to use List<>, can I use it for two dimensions?

如何定义二维动态数组?如果我想使用List<>,我可以将它用于二维吗?

采纳答案by Mark Seemann

There's no built-in dynamic equivalent of two-dimensional arrays that I'm aware of, but you can easily get at more or less the same functionaltiy.

我所知道的二维数组没有内置的动态等效项,但是您可以轻松地获得或多或少相同的功能。

Define a Coordinate class with this API:

用这个 API 定义一个 Coordinate 类:

public class Coordinate : IEquatable<Coordinate>
{
     public Coordinate(int x, int y);
     public int X { get; }
     public int Y { get; }
     // override Equals and GetHashcode...
}

You can now create a collection of these Coordinate instances.

您现在可以创建这些 Coordinate 实例的集合。

If you create a HashSet<Coordinate>you will be guaranteed that you cannot add a Coordinate if it's already added because it overrides Equals.

如果你创建了一个,HashSet<Coordinate>你将保证你不能添加一个已经添加的坐标,因为它覆盖了 Equals。

If you want, you can expand Coordinate to Coordinate<T>like this:

如果你愿意,你可以Coordinate<T>像这样展开坐标:

public class Coordinate<T> //...
{
    // previous stuff...

    public T Item { get; set; }
}

This will allow you to associate a strongly typed item with each coordinate, like this:

这将允许您将强类型项与每个坐标相关联,如下所示:

var items = new HashSet<Coordinate<string>>();
items.Add(new Coordinate<string>(1, 4) { Item = "Foo" });
items.Add(new Coordinate<string>(7, 19) { Item = "Foo" });
// ...

回答by Konamiman

You should take a look here: Arrays Tutorial in C#. Anyway here is the syntax: type[,] namefor multidimensional arrays, type[][] namefor jagged arrays (change typefor the type of the objects to be stored in the array).

您应该在这里查看:C# 中的数组教程。无论如何这里是语法:type[,] name对于多维数组,type[][] name对于锯齿状数组(更改type要存储在数组中的对象的类型)。

回答by Gerrie Schenck

This would be something like this:

这将是这样的:

List<List<string>> twoDimensional = new List<List<string>>();

But I think it is impractical to use a List for this, better resort to arrays...

但是我认为为此使用 List 是不切实际的,最好使用数组...

回答by leppie

With arrays you cant, they are never 'dynamic'.

对于不能使用的数组,它们永远不是“动态的”。

With lists you cant either (unless you make it look like one by providing an additional indexer), unless you are simply looking for a list of lists (which is not multi-dimensional).

对于列表,您也不能(除非您通过提供额外的索引器使其看起来像一个),除非您只是在寻找列表列表(不是多维的)。

That would be (for a list of list of string):

那将是(对于字符串列表的列表):

List<List<string>>

回答by Yin Zhu

you can use list e.g. List> and list in c# is very fast.

您可以使用列表,例如 List> 并且在 C# 中的列表非常快。

but by two-d array you need something like:

但是通过二维数组,您需要类似的东西:

int[,] arr = new int[100,100];

and this should be the more faster than list.

这应该比列表更快。