2011-06-13 129 views
2

在我的代码中我有一个名为数组的ArrayList。c#搜索arraylist

我已经用数字填满它从1到13

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

在我的代码后来我也随意去除一些元素。 示例array.remove(3);

现在我想要搜索,arraylist中的元素 有多少个值超过了特定的数字。

那么,有多少个元素ArrayList中例如超过5

任何人谁知道如何做到这一点?

谢谢!

+8

为什么要使用ArrayList?使用'List '这将会容易得多。 – 2011-06-13 11:34:23

+0

我想在这个项目中使用数组列表。谢谢你的提示! – laz 2011-06-13 11:39:56

回答

6

使用该lambda表达式:

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

或更简单:

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

你必须从Java吗?我相信你应该在c#中使用List<T>

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

非常感谢! – laz 2011-06-13 11:51:54

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

或更改您的ArrayList是一个枚举允许。

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

谢谢,它的工作! – laz 2011-06-13 11:50:48