C#中的多维数组列表或列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1596530/
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
Multi-dimensional arraylist or list in C#?
提问by etoisarobot
Is it possible to create a multidimensional list in C#? I can create an multidimensional array like so:
是否可以在 C# 中创建多维列表?我可以像这样创建一个多维数组:
string[,] results = new string[20, 2];
But I would like to be able to use some of the features in a list or arraylist like being able to add and delete elements.
但是我希望能够使用列表或数组列表中的某些功能,例如能够添加和删除元素。
采纳答案by Charles Bretana
You can create a list of lists
您可以创建列表列表
public class MultiDimList: List<List<string>> { }
or a Dictionary of key-accessible Lists
或键可访问列表字典
public class MultiDimDictList: Dictionary<string, List<int>> { }
MultiDimDictList myDicList = new MultiDimDictList ();
myDicList.Add("ages", new List<int>());
myDicList.Add("Salaries", new List<int>());
myDicList.Add("AccountIds", new List<int>());
Generic versions, to implement suggestion in comment from @user420667
通用版本,在@user420667 的评论中实施建议
public class MultiDimList<T>: List<List<T>> { }
and for the dictionary,
而对于字典,
public class MultiDimDictList<K, T>: Dictionary<K, List<T>> { }
// to use it, in client code
var myDicList = new MultiDimDictList<string, int> ();
myDicList.Add("ages", new List<T>());
myDicList["ages"].Add(23);
myDicList["ages"].Add(32);
myDicList["ages"].Add(18);
myDicList.Add("salaries", new List<T>());
myDicList["salaries"].Add(80000);
myDicList["salaries"].Add(100000);
myDicList.Add("accountIds", new List<T>());
myDicList["accountIds"].Add(321123);
myDicList["accountIds"].Add(342653);
or, even better, ...
或者,更好的是,...
public class MultiDimDictList<K, T>: Dictionary<K, List<T>>
{
public void Add(K key, T addObject)
{
if(!ContainsKey(key)) Add(key, new List<T>());
if (!base[key].Contains(addObject)) base[key].Add(addObject);
}
}
// and to use it, in client code
var myDicList = new MultiDimDictList<string, int> ();
myDicList.Add("ages", 23);
myDicList.Add("ages", 32);
myDicList.Add("ages", 18);
myDicList.Add("salaries", 80000);
myDicList.Add("salaries", 110000);
myDicList.Add("accountIds", 321123);
myDicList.Add("accountIds", 342653);
EDIT: to include an Add() method for nested instance:
编辑:为嵌套实例包含一个 Add() 方法:
public class NestedMultiDimDictList<K, K2, T>:
MultiDimDictList<K, MultiDimDictList<K2, T>>:
{
public void Add(K key, K2 key2, T addObject)
{
if(!ContainsKey(key)) Add(key,
new MultiDimDictList<K2, T>());
if (!base[key].Contains(key2))
base[key].Add(key2, addObject);
}
}
回答by Mehrdad Afshari
Not exactly. But you can create a list of lists:
不完全是。但是您可以创建一个列表列表:
var ll = new List<List<int>>();
for(int i = 0; i < 10; ++i) {
var l = new List<int>();
ll.Add(l);
}
回答by Joseph
you just make a list of lists like so:
您只需列出如下列表:
List<List<string>> results = new List<List<string>>();
and then it's just a matter of using the functionality you want
然后这只是使用你想要的功能的问题
results.Add(new List<string>()); //adds a new list to your list of lists
results[0].Add("this is a string"); //adds a string to the first list
results[0][0]; //gets the first string in your first list
回答by John Gietzen
Depending on your exact requirements, you may do best with a jagged array of sorts with:
根据您的确切要求,您最好使用各种锯齿状数组:
List<string>[] results = new { new List<string>(), new List<string>() };
Or you may do well with a list of lists or some other such construct.
或者,您可以很好地处理列表列表或其他一些此类构造。
回答by csharptest.net
If you want to modify this I'd go with either of the following:
如果你想修改它,我会选择以下任一方法:
List<string[]> results;
-- or --
- 或者 -
List<List<string>> results;
depending on your needs...
根据您的需要...