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?

6 Years
Discussion Span
Last Post by Geekitygeek

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

Edited by ddanbe: n/a



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>();

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
    //item not in list
    //add item

*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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.