多维列表C# [英] Multidimensional List C#

查看:74
本文介绍了多维列表C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个多维保护列表,分别列出X,Y和Z三个值,因为需要查询该值,因此必须删除该数组.

I need to create a multidimensional guard List three values​​, X, Y and Z, and I need a List that is because once the value is queried, the array must be removed.

查询看起来像这样:列表[0] [0] = X,列表[0] [a] = Y,列表[0] [2] = X,因此我只能删除索引0并且他已经删除了所有其他三个.

The query would look something like this: List [0] [0] = X, List [0] [a] = Y and List [0] [2] = X, so that I can remove only the index 0 and he already remove all the other three.

推荐答案

如果您需要创建多维列表,则始终可以像这样创建列表列表:

If you need to create a multidimensional list, you can always create a list of lists like so:

var multiDimensionalList = new List<List<string>>{
    new List<string>{"A","B","C"},
    new List<string>{"D","E","F"},
    new List<string>{"G","H","I"},
};
Console.WriteLine(multiDimensionalList[2][1]); // Prints H

multiDimensionalList[2].RemoveAt(1);
Console.WriteLine(multiDimensionalList[2][1]); // Prints I

multiDimensionalList[2][1] = "Q";
Console.WriteLine(multiDimensionalList[2][1]); // Prints Q

请注意,尽管尝试通过赋值方式替换不存在的值将引发异常:

Be aware though that attempting to replace a value that doesn't exist by way of assignment will throw an exception:

multiDimensionalList[2][5] = "R"; // Throws an ArgumentOutOfRangeException

这篇关于多维列表C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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