I'm trying to make the following:

Step one : Check if the values have been stored more then x time, if so delete them.
Step two : Check if current value has been used before
Step Three : If used return False (and write value to arraylist), else return true (dont' write the value to the arraylist).

I thought this would be best if I create an arrayList. But how do i go about this?

Recommended Answers

All 3 Replies

Use List (generic) instead of ArrayList.
Use the Contains method to check if an object is already in the list.

Hi,

When you say you want to check if they have been "stored more than x times", do you mean in the arraylist? If you are only allowing values to be stored if they havent been used then this check is redundant (unless you plan to populate the arraylist with existing that may contain duplicates).
Also, at step 2, do you mean you want to check if the value exists in the array list already?
If you are only going to store a single datatype then i'd recommend the List over the ArrayList. The following use a List of integers. The FindAll method uses a predicate search to return a new list containing all the matches:

List<int> intList = new List<int>();
intList.Add(1);
intList.Add(2);

int value = 3; //value to search for

List<int> matches = intList.FindAll(delegate (int i) { return i == value; });

if (matches.Count > 0)
{
    //item already in list
}
else
{
    //item not in list
    //add item
    intList.Add(value);
}

*palms face* of course, if you just want to check if it is in the list then ddanbe is correct....Contains() is a much simpler method :)
Mine will allow you to retrieve all instances of the value and then alter/delete them as you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.