阵列列表中的重复项 [英] Duplicates in a Arraylist

查看:89
本文介绍了阵列列表中的重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早安全部

我有一个Arralist,例如

Good Morning ALL

I have an Arralist for example

<br />1<br />2<br />3<br />4<br />5<br />2<br />4<br />3



现在我需要在此数组列表中查找重复项.如何获得重复的


谢谢



Now i need to Find Duplicates in this array list. how can i get the Duplicated


Thank you

推荐答案

ArrayList 太旧了,请尝试避免除非您使用.NET 1.1.

方法1
ArrayList is too old and try to avoid unless you are on .NET 1.1.

Method 1
list.Sort();<br />int i = 0;<br />while (i < list.Count - 1)<br />{<br />   if ((int)list[i] == (int)list[i + 1])<br />        list.RemoveAt(i);<br />   else<br />        ++i;<br />}

方法2

ArrayList target = new ArrayList(list.ToArray().Distinct().ToArray());

仅当导入了System.Linq名称空间时,此方法才有效.

如果使用.NET 3.5,请使用HashSet(T)类.它将不允许创建重复的条目.对于.NET 2.0或更高版本,请使用ArrayList以外的其他List(T).

:)

This works only when you have System.Linq namespace imported.

If you use .NET 3.5, use HashSet(T) class. It will not allow duplicate entries to be created. For .NET 2.0 or later, use List(T) other than ArrayList.

:)


您应该从信号标记中取出"C#",直到您学会它为止.

已被问到/answer至少两次.这是家庭作业,不是吗?

You should take "C#" out of your sig until you learn it.

This has been asked/answered at least twice before. It''s homework, isn''t it?


这篇关于阵列列表中的重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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