如何编写一个有效的算法来设置项集合上的标志 [英] How to write an efficient algorithm to set the flag on items collection
问题描述
让我们说有一个项目的集合
当项目1添加类型杂货店超过1次然后我需要显示基于一些布尔标志的弹出图标isShowIcon = true / false。
当项目1添加类型杂货店(没有信息弹出图标)
当项目2添加了类型杂货店(显示信息弹出图标)
当项目3添加类型化妆品(没有信息弹出图标)
当项目4添加类型杂货店时(显示信息弹出图标)
删除项目1然后
项目2(无信息弹出图标)
项目4(显示信息弹出图标)
删除项目2然后
项目4(无信息弹出图标)
我尝试了什么:
我尝试了几种逻辑,但无法达到要求。
Lets say There is a collection of Items
When item 1 added with type "grocery" more than 1 times then i need to show the popup icon on the basis of some boolean flag "isShowIcon = true/false".
when item 1 added with type "grocery" (no information popup icon)
when item 2 added with type "grocery" (show information popup icon)
when item 3 added with type "cosmetics" (no information popup icon)
when item 4 added with type "grocery" (show information popup icon)
Delete item 1 then
item 2 (no information popup icon)
item 4 (show information popup icon)
Delete item 2 then
item 4 (no information popup icon)
What I have tried:
I have tried several logic's but unable to accomplish the requirement.
推荐答案
在添加新项目之前,做一些事情l我喜欢这个:
Before the new item is added, do something like this:
var found = mycollection.FirstOrDefault(x=>x.name == text);
if (found == null)
{
mycollection.Add(text);
}
else
{
MessageBox.Show("Duplicate names are not allowed.");
}
ASystem.Collections.Generic.HashSet< T>
是典型的检测重复。基本上,你做以下......
ASystem.Collections.Generic.HashSet<T>
is typical for detecting duplicates. Basically, you do the following...
string text = "Some Text";
var mySet = new HashSet<string>();
if (mySet.Contains(text))
Console.WriteLine("Duplicate!");
else
mySet.Add(text);
这篇关于如何编写一个有效的算法来设置项集合上的标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!