Skip to content

Arraylist problem

Thomas Richard

Asked Jul 25, 2006, 3:50 AM EDT1 answer
Share
LinkedInX
Hey

i'm still pretty new to C# so.. I'll try to explain my problem.

I have and Arraylist wich contains a certain amount of objects, wich i call articles. Each of these articles has his own Article ID or shorter aid.

Now i'd like to check if a certain article with that ID is already assigned to the arraylist.

Here is some code:

//Declaring arraylist private ArrayList artikelen; //Adding an article to the arraylist public void addArtikel(int aid) { //The aid constructor is used, wich gets some values from a database according to this aid. artikelen.Add(new Artikel(aid)); } //Checking if the article already exists in the arraylist? public Boolean articleExists(int aid) { artikelen.BinarySearch ???? }

Thanks Thomas

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I am guessing the arraylist is sorted?  a binary search requires the arraylist to be sorted. 
    you can use the .contain method. although if your list is sorted it would be much slower than a binary search. 
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsarraylistclasscontainstopic.asp
    msdn is the best source for looking up classes and getting sample code.


    You might want to post your code as it is writen so it is easier to understand. i am guessing it looks like this.

    //Declaring arraylist
    private ArrayList artikelen;

    //Adding an article to the arraylist
    public void addArtikel(int aid) {
    //The aid constructor is used, wich gets some values from a database
    //according to this aid.

    artikelen.Add(new Artikel(aid));
    }
    //Checking if the article already exists in the arraylist?
    public Boolean articleExists(int aid)
    { artikelen.BinarySearch ???? }
    0