Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 定义二维动态数组_C#_.net_Arrays - Fatal编程技术网

C# 定义二维动态数组

C# 定义二维动态数组,c#,.net,arrays,C#,.net,Arrays,如何定义二维动态数组? 如果我想使用列表,我可以将其用于二维吗?您应该看看这里:。无论如何,这里是语法:type[,]name用于多维数组,type[]name用于锯齿数组(更改type用于存储在数组中的对象的类型)。这类似于: List<List<string>> twoDimensional = new List<List<string>>(); public class Coordinate<T> //... { //

如何定义二维动态数组?
如果我想使用列表,我可以将其用于二维吗?

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

这类似于:

List<List<string>> twoDimensional = new List<List<string>>();
public class Coordinate<T> //...
{
    // previous stuff...

    public T Item { get; set; }
}
var items = new HashSet<Coordinate<string>>();
items.Add(new Coordinate<string>(1, 4) { Item = "Foo" });
items.Add(new Coordinate<string>(7, 19) { Item = "Foo" });
// ...
List twodional=新列表();

但是我认为使用列表是不切实际的,最好使用数组…

对于不能使用的数组,它们从来都不是“动态的”

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

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

列表

您可以使用list,例如list>,而c#中的list速度非常快

但通过二维阵列,您需要类似以下内容:

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

这应该是比列表更快的

据我所知,二维数组没有内置的动态等价物,但您可以轻松获得大致相同的功能

使用此API定义坐标类:

public class Coordinate : IEquatable<Coordinate>
{
     public Coordinate(int x, int y);
     public int X { get; }
     public int Y { get; }
     // override Equals and GetHashcode...
}
公共类坐标:I可调
{
公共坐标(整数x,整数y);
公共整数X{get;}
公共整数Y{get;}
//重写Equals和GetHashcode。。。
}
现在可以创建这些坐标实例的集合

如果您创建一个
哈希集
,您将保证如果坐标已经添加,则无法添加坐标,因为它覆盖了Equals

如果需要,可以将坐标展开为
坐标
,如下所示:

List<List<string>> twoDimensional = new List<List<string>>();
public class Coordinate<T> //...
{
    // previous stuff...

    public T Item { get; set; }
}
var items = new HashSet<Coordinate<string>>();
items.Add(new Coordinate<string>(1, 4) { Item = "Foo" });
items.Add(new Coordinate<string>(7, 19) { Item = "Foo" });
// ...
公共类坐标//。。。
{
//以前的东西。。。
公共T项{get;set;}
}
这将允许您将强类型项与每个坐标关联,如下所示:

List<List<string>> twoDimensional = new List<List<string>>();
public class Coordinate<T> //...
{
    // previous stuff...

    public T Item { get; set; }
}
var items = new HashSet<Coordinate<string>>();
items.Add(new Coordinate<string>(1, 4) { Item = "Foo" });
items.Add(new Coordinate<string>(7, 19) { Item = "Foo" });
// ...
var items=newhashset();
添加(新坐标(1,4){Item=“Foo”});
添加(新坐标(7,19){Item=“Foo”});
// ...

这只是一个列表列表。不是二维的。-1这相当于锯齿状数组,不是二维数组。嗯,你说得对。。。。我们应该有更多关于问题背景的信息,他为什么需要这些信息等等。请更详细地告诉我们你的情况。在某些情况下,您不需要二维数组,而需要字典或类似的东西。在声明数组后,如何添加到数组中?我尝试了此解决方案,不知道是否是因为在较新版本的c中情况发生了变化,但这些是我遇到的错误<代码>'Coordinate'不实现接口成员'IEquatable.Equals(Coordinate)以及
'Coordinate.Coordinate(int,int)'必须声明一个主体,因为它没有标记为抽象、外部或部分
以及
使用泛型类型'Coordinate'需要1个类型参数