2

In my code i have an arraylist called array.

I have filled it with numbers from 1 to 13

for(int i =1; i< 14; i++)
{
  array.items.Add(i)
}

Later in my code I also remove some of the elements at random. Example array.remove(3);

Now I wanna search, how many values of the elements in the arraylist is over specific number.

So, how many elements in the arraylist is over for example 5.

Anyone who knows how to do this?

Thanks!

2
  • 8
    Why use an ArrayList? This would be much easier with a List<int>. Commented Jun 13, 2011 at 11:34
  • I would like to use an arraylist in this project. Thanks for your tip though! Commented Jun 13, 2011 at 11:39

3 Answers 3

5

Use this lambda expression:

int count = array.Cast<int>().Where(e=> e > 5).Count();

or even simpler:

int count = array.Cast<int>().Count(e=> e > 5);

You must be from Java right? I believe that you should use a List<T> in c#.

Sign up to request clarification or add additional context in comments.

Comments

1
int count = array.Cast<int>().Count(x => x > 5);

OR change your arrayList to be an enumerable to allow.

int count = array.Count(x => x > 5);

Comments

0
array.Cast<int>().Where(item => item > 5).Count();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.