ArrayList概念 [英] ArrayList concepet

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

问题描述

大家好,

我是Nagarajan软件解决方案开发人员.我有一个疑问.
我正在使用数据网格控件,并检索了电子邮件文本框项目,并将其存储到数组列表中.
如何删除数组列表中的重复元素?
我正在使用ASP.net和C#.
请看一下并回复.

谢谢!

问候,

Nagarajan P.

Hi guys,

I am Nagarajan a software solution developer. I have one doubt.
I am using a data grid control, and retrieved email textbox item, and stored it into the array list.
How can I delete duplicate elements in the array list?
I''m using ASP.net and C#.
Kindly look at this and reply.

Thank you!

Regards,

Nagarajan P.

推荐答案

创建一个名为AddIfUnique的扩展方法.此扩展方法将为您执行唯一性检查,而您要做的只是传递要添加的数据.我一直都在做这种事情.
Create an extension method called AddIfUnique. This extension method would do the uniqueness check for you, and all you have to do is pass in the data you want to add. I do this kind of thing all the time.


嗨Nagarajan,

当您将元素添加到ArrayList调用时,包含方法将检查该元素是否已在列表中.仅当元素不包含在列表中时,才将其添加到所述ArrayList中.请参阅下面的示例代码:

Hi Nagarajan,

when you are adding the elements to the ArrayList call the method Contains to check if that element is already in the list. Add the element to said ArrayList only if it is not contained in the list. See my example code below:

pulic bool function AddIfNotContained(Object element, ArrayList list)
{
    bool wasAdded = false;
    if(!list.Contains(element))
    {
        list.Add(element);
        wasAdded = true;
    }
    return wasAdded;
}



那应该解决您的问题.

问候,

曼弗雷德(Manfred)



That should solve your problem.

Regards,

Manfred


为什么不通过不允许第一个列表中的Array重复来在源头解决问题?

否则,这里有许多不同的解决方案

http://stackoverflow.com/questions/9673/remove-duplicates-from-array [ ^ ]

HashSet示例很好,取决于您是否使用> = .Net 3.5
Why not stop the problem at the source by not allowing duplicates in the Array in the first list?

Otherwise, lots of different solutions here

http://stackoverflow.com/questions/9673/remove-duplicates-from-array[^]

The HashSet example is nice, depends if you''re on >= .Net 3.5


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

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