RV.
RV.

Reputation: 2842

arraylist count

hi i have more than 10 arraylist in my project, i need to compare all arraylist for their count(same or not). here i need shortest way to find that. am not sorting i need to find length of all arraylist count(same or not).

Upvotes: 0

Views: 5960

Answers (3)

Umair Ahmed
Umair Ahmed

Reputation: 11694

It would be better if you add all those arraylist in another arraylist and then run an iterator over it comparing length.

you have 10 variables, and you performing some task on all of them, a good candidate for a collection

Upvotes: 3

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

Put them in an array and compare their counts:

private static bool CountsAreEqual(ICollection[] lists)
{
    int previousCount = lists[0].Count;
    for (int i = 1; i < lists.Count; i++)
    {
        if (lists[i].Count != previousCount)
        {
            return false;
        }
    }
    return true;
}

Used like so:

ArrayList arr1 = GetFirstList();
ArrayList arr2 = GetSecondList();
CountsAreEqual(new[] {arr1, arr2});

Upvotes: 3

Coxy
Coxy

Reputation: 8928

Can't you simply use the .Count property to see how many elements are contained within the ArrayList?

Or are you asking for an algorithm to find out which ArrayLists out of a collection of 10 have the same number of elements contained in them?

Upvotes: 1

Related Questions