什么是集等效的多维阵列的? [英] What is the collection equivalent of a multi-dimensional array?
问题描述
我有一组数据,看起来像这样:
I've got a group of data that looks like this:
001 001 One
001 002 Two
001 003 Three
002 001 One
002 002 Two
002 003 Three
...
现在,当然,我可以创建一个字符串数组[X] [Y] = Z,但这种阵列是可调整大小,而我更愿意使用索引的字符串表示比转换为数值。其原因是,我需要通过字符串查找数据,我没有看到在不用与字符串>数转换点。
Now, certainly, I could create an array of string[x][y] = z, but this array has to be resizable, and i'd prefer to use the string representations of the indexers than convert to numeric. The reason is that i will need to look up the data by string, and i don't see the point in needless string->number conversions.
我的第一个念头是这样的:
My first thought was this:
Dictionary<string, Dictionary<string, string>> data;
data = new Dictionary<string, Dictionary<string, string>>();
Dictionary<string, string> subdata = Dictionary<string, string>();
subdata.Add(key, string);
data.add(key2, subdata);
和这个工作,但有些繁琐。它也觉得不妥和缺憾,并没有特别有效的。
and this works, but is somewhat cumbersome. It also feels wrong and kludgy and not particularly efficient.
那么什么是这类数据的存储集合中的最佳方式?
So what's the best way to store this sort of data in a collection?
我也想创造我自己的集合类的,但我宁愿没有,如果我没有。我宁愿只使用现有的工具。
I also thought of creating my own collection class, but I'd rather not if I don't have to. I'd rather just use the existing tools.
推荐答案
这是很常见的要求,大多数人写出来的一些变化一个元组类。如果你正在使用ASP.Net,您可以利用三
类是已经上市,否则,写类似:
This is pretty common request, and most people end up writing some variation of a Tuple class. If you're using ASP.Net, you can utilize the Triple
class that's already available, otherwise, write something like:
public class Tuple<T, T2, T3>
{
public Tuple(T first, T2 second, T3 third)
{
First = first;
Second = second;
Third = third;
}
public T First { get; set; }
public T2 Second { get; set; }
public T3 Third { get; set; }
}
有一个通用的三元组类,这样你就可以创建一个新的列表与LT元组LT;字符串,字符串,字符串>>()
并创建您的元组和添加。展开与一些索引功能,基础类,你就起来了,远离
There's a generic three-tuple class, so you can create a new List<Tuple<string, string, string>>()
and create your tuples and add them. Expand on that basic class with some indexing functionality and you're up up and away.
编辑:有一本字典的名单似乎并不像正确的做法,因为每个字典只拿着一个值。还有就是关键和价值之间没有多次入境的关系 - 是根本一种多部分键和一个关联的值。该数据相当于一个数据库行(!或元组)
A list with a dictionary doesn't seem like the correct approach, because each dictionary is only holding one value. There is no multi-entry relationship between the key and values - there is simply one multi-part key and one associated value. The data is equivalent to a database row (or tuple!).
EDIT2:这里是你可以用方便的可转位列表类
Here's an indexable list class you could use for convenience.
public class MyTupleList : List<Tuple<string, string, string>>
{
public Tuple<string, string, string> this[string first, string second]
{
get
{
return (this.Find(x => x.First == first && x.Second == second));
}
set
{
this[first, second] = value;
}
}
}
这篇关于什么是集等效的多维阵列的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!