0

如何在 dotnet 框架 2 中合并 ArrayList C# 中的数据?

example of data : 1, 2, 2, 3, 4, 5, 5, 6, 6
how to get 1, 2, 3, 4, 5, 6
4

3 回答 3

5
Hashtable htCopy = new Hashtable();

foreach (int item in arrListFull) 
{   
    htCopy[item] = null;
}

ArrayList distinctArrayList = new ArrayList(htCopy.Keys);
于 2009-09-15T05:02:31.623 回答
2
// Assuming your data is an ArrayList called "source"
ArrayList dest = new ArrayList();
foreach(int i in source) if(!dest.Contains(i)) dest.Add(i);

不过,您应该使用 List<int> 而不是 ArrayList。

编辑:使用 Sort+BinarySearch 的替代解决方案,如 Kobi 所建议的:

// Assuming your data is an ArrayList called "source"
source.Sort();
ArrayList dest = new ArrayList();
foreach (int i in source) if (dest.BinarySearch(i)<0) dest.Add(i);
于 2009-09-15T05:06:20.163 回答
0
public ArrayList RemoveDups ( ArrayList input )
{
    ArrayList single_values = new ArrayList();

    foreach( object item in input)
    {
        if( !single_values.Contains(item) )
        {
            single_values.Add(item);
        }
    }
    return single_values;
}
于 2009-09-15T05:08:59.550 回答