use*_*315 8 c# dictionary loops arraylist hashset
关于:我的数学程序将有一个巨大的项目集合来迭代.它主要由一个项和一个指向另一个项((int)item,(int)指针)的指针组成,类似于一个键值对.但是,每个项目都有其他几个属性,如下所示:
(项目,指针),(属性,属性,属性......)
项目,指针和属性将经常添加.
搜索这个网站和其他网站让我对这个程序使用的集合更加无能为力.
此时在vcskicks.com上找到的词典解决方案似乎效果最好:
Dictionary<Dictionary<int,int>,Dictionary<int,int> nestedDictionary =
new Dictionary<Dictionary<int,int>,Dictionary<int,int> nestedDictionary();
Run Code Online (Sandbox Code Playgroud)
或用简明的语言:
Dictionary<Dictionary<item, pointer>,
Dictionary<attribute,attribute, ...> nestedDictionary =
Dictionary<Dictionary<item, pointer>,
Dictionary<attribute,attribute, ...>();
Run Code Online (Sandbox Code Playgroud)
请注意,属性数量未预定义,长度不同.此时此刻由于性能开销,我不愿意使用对象.
Hashsets似乎不适合,因为重复的项目将存在,但它们将具有不同的属性.或者一个hashset可以有重复的项目,但只是没有重复的hashkeys?似乎有些混乱.
根据一些以下hashset将无法计算:
11011, 0001
11011, 0011
Run Code Online (Sandbox Code Playgroud)
根据其他人的意见,因为它将有一个不同的哈希键.这让我感到困惑.
我的问题:
有可能变得模糊:使用哪种收集类型最好?如有必要,我很乐意为故事添加更多内容.
编辑:
巨人意味着:可能有数百万件物品.所有项目都有一个指针和属性. 一般用途是搜索特定项目,检索指针,获取下一个指针的项目,直到没有指针为止.同时收集每个项目的所有属性.添加将定期进行,仅偶尔删除. 指针:指针是它引用的项目的索引.如果您有2个项目,并且第一个项目链接到第二个项目,则第一个项目将第二个项目的索引作为指针. 最佳定义为内存使用和速度.最后,所有找到的项目将相互比较. 例:
[Item , pointer] [attribute, attribute, ...]
[11011, 1001] [ 1101, 1111 ]
[10001, 1000] [ 1110, 0101 ]
[11111, 0010] [ 1111, 1110 ]
[11011, 0001] [ 0010, 1010 ]
Run Code Online (Sandbox Code Playgroud)
谢谢
因此,基本上,您似乎需要保留一个对象集合,每个对象都具有以下属性:
因此,一个项目看起来像这样(只是一个简单的例子......这并不是将所有内容公开的最佳实践,但在你的情况下你不会介意):
public struct MyItem
{
public Dictionary<String, String> attributes;
public MyItem next;
}
Run Code Online (Sandbox Code Playgroud)
那么你所需要的就是保留一个列表:
List<MyItem> myList;
Run Code Online (Sandbox Code Playgroud)
当你想添加一些东西时,很简单:
MyItem item1 = new MyItem();
item1.attributes["name"] = "Joe";
item1.next = null; // this is the default behaviour... just illustrating here
myList.Add(item1);
MyItem item2 = new MyItem();
item2.attributes["name"] = "Mary";
item2.next = item1;
myList.Add(item2);
Run Code Online (Sandbox Code Playgroud)
然后你想穿越的时候就跟着next就可以了。
MyItem item = myList[0];
while (item != NULL)
{
Console.WriteLine(item["name"]);
item = item.next;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
352 次 |
| 最近记录: |