简体   繁体   English

关于 2D 列表 C# 的问题

[英]Questions about 2D lists C#

I've ran into an issue with lists as I need to create a 2-dimensional list where I can read data by giving the columns and rows, so I could read from my list by using my_List[col][row] so is it possible making a 2D list that way?我遇到了列表问题,因为我需要创建一个二维列表,我可以在其中通过提供列和行来读取数据,因此我可以使用my_List[col][row]从我的列表中读取数据,所以是这样可以这样制作二维列表吗?

How much performance impact can this have and anything I should be aware that could have a performance impact on the code?这会对性能产生多大影响,以及我应该注意的可能对代码产生性能影响的任何事情? I might need to read a few hundred times per second from my 2D list我可能需要每秒从我的 2D 列表中读取数百次

Is it possible to have a more grid type 2D list so if i have data in 3, 4 and 5 but i dont have anything in 0, 1, and 2 think of it like coordinates.是否可以有更多网格类型的 2D 列表,所以如果我在 3、4 和 5 中有数据,但在 0、1 和 2 中没有任何数据,请将其视为坐标。 so can I read from the list using myList[3][5] and get the data from there with 0, 1 and 2 having nothing?那么我可以使用myList[3][5]从列表中读取数据并从那里获取 0、1 和 2 什么都没有的数据吗? or do i need to loop it through and add something there like null?还是我需要循环它并在那里添加一些东西,比如空?

thanks in advance!提前致谢!

Yes, you can indeed use multidimensional arrays or jagged arrays for storing "2D data".是的,您确实可以使用多维数组或锯齿状数组来存储“二维数据”。

As for creating a data structure that doesn't use any memory space for unused indexes, an option could be to use a dictionary where the keys are tuples of two numbers, like this (assuming that your data are strings):至于为未使用的索引创建不使用任何内存空间的数据结构,一个选项可能是使用字典,其中键是两个数字的元组,如下所示(假设您的数据是字符串):

var items = new Dictionary<(int, int), string>();

items.Add((0,1), "0-1"); //this throws an error if the key already exists
items[(2,3)] = "2-3";    //this silently replaces the value if the key already exists

Console.WriteLine(items.Keys.Contains((0,1))); //true
Console.WriteLine(items.Keys.Contains((0,2))); //false

Console.WriteLine(items[(2,3)]); //"2-3"

Of course you probably want to encapsulate this functionality in its own class, but you get the idea.当然,您可能希望将此功能封装在它自己的类中,但您明白了。

Note however that this dictionary approach will probably be worse than a plain array in terms of performance, but it's up to you to experiment and collect some metrics.但是请注意,就性能而言,这种字典方法可能比普通数组更差,但您可以自行试验并收集一些指标。

You can create 2D Arrays like this :您可以像这样创建二维数组:

string[,] twoDArray = new string[2,2];

Then you can loop through it like :然后你可以像这样循环遍历它:

        for (int i = 0; i < twoDArray.Length; i++)
        {
            foreach (int j in twoDArray[i,0])
            {

            }
        }

You can also create 2D Lists like this:您还可以像这样创建 2D 列表:

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

and iterate through them using an Enumerator and for example a for loop:并使用 Enumerator 迭代它们,例如 for 循环:

 var enu = grid.GetEnumerator();
            while (enu.MoveNext())
            {
                for(int i = 0; i < enu.Current.Count; i++)
                {
                    enu.Current.RemoveAt(i);
                }
            }

You are basically iterating over all lists and then through each list as long as its size is.您基本上是遍历所有列表,然后遍历每个列表,只要其大小不变。 Inside the for loop you can alter the encapsuled lists in whatever way you like.在 for 循环中,您可以按照您喜欢的任何方式更改封装列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM