juli said:
Hello,
I will try to define my problem better: I have a collection that is made
from a text file (long one) and it will take too much time to go through
all the collection.
I have there a string variable (one of collection object members) I want
to have a string array wich will contain all those string only once
(distinctly).How can I do it?
Thanks
I reckon you want something like the Java Set interface. Below is a
simple implementation of a set I developed some time ago. This class
acts like an ArrayList, but it does not allow duplicate entries.
using System;
using System.Collections;
namespace Objectware.Collections {
public class Set : ICollection {
private ArrayList innerList=new ArrayList();
public Set() {}
public void CopyTo(Array array) {
CopyTo(array, 0);
}
public void CopyTo(Array array, int index) {
innerList.CopyTo(array, index);
}
public int Count {
get { return innerList.Count; }
}
public object SyncRoot {
get { return innerList.SyncRoot; }
}
public bool IsSynchronized {
get { return innerList.IsSynchronized; }
}
public int Add(object value) {
if (!Contains(value)) {
return innerList.Add(value);
} else return -1;
}
public void AddAll(ICollection collection) {
foreach (object item in collection) {
Add(item);
}
}
public void Remove(object value) {
innerList.Remove(value);
}
public void RemoveAll(ICollection collection)
{
foreach (object item in collection)
{
Remove(item);
}
}
public bool RetainAll(ICollection collection) {
ArrayList newList=new ArrayList(innerList);
foreach (object item in collection) {
newList.Remove(item);
}
int oldCount=innerList.Count;
innerList=newList;
return oldCount!=innerList.Count;
}
public bool Contains(object value) {
return innerList.Contains(value);
}
public bool ContainsAll(ICollection collection) {
foreach (object item in collection) {
if (!Contains(item)) return false;
}
return true;
}
public void Clear() {
innerList.Clear();
}
public IEnumerator GetEnumerator() {
return innerList.GetEnumerator();
}
}
}
Anders Norås
http://dotnetjunkies.com/weblog/anoras/