在 C# 中过滤数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1912128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:47:09  来源:igfitidea点击:

filter an array in C#

c#arrays

提问by leora

i have an array of objects (Car[] for example) and there is an IsAvailable Property on the object

我有一个对象数组(例如 Car[]),并且对象上有一个 IsAvailable 属性

i want to use the full array (where IsAvailable is true for some items and false for some others) as the input and return a new array which includes only the items that have IsAvailable = true.

我想使用完整数组(其中 IsAvailable 对于某些项目为 true,对于其他一些项目为 false)作为输入并返回一个新数组,其中仅包含 IsAvailable = true 的项目。

采纳答案by Justin Niessner

If you're using C# 3.0 or better...

如果您使用的是 C# 3.0 或更高版本...

using System.Linq;

public Car[] Filter(Car[] input)
{
    return input.Where(c => c.IsAvailable).ToArray();
}

And if you don't have access to LINQ (you're using an older version of .NET)...

如果您无权访问 LINQ(您使用的是旧版本的 .NET)...

public Car[] Filter(Car[] input)
{
    List<Car> availableCars = new List<Car>();

    foreach(Car c in input)
    {
        if(c.IsAvailable)
            availableCars.Add(c);
    }

    return availableCars.ToArray();
}

回答by LorenVS

Easiest way:

最简单的方法:

Car[] cars = //...
Car[] filtered = cars.Where(c => c.IsAvailable).ToArray();

Possibly More Efficient:

可能更高效:

Car [] cars = //...
    List<Car> filteredList = new List<Car>();
    for(int i = 0; i < cars.Length; i++)
    {
        if(cars[i].IsAvailable)
           filteredList.Add(cars[i]);
    }
    Car[] filtered = filteredList.ToArray();

回答by goatlinks

A simple solution is to create a new array, loop through the input array and add only those items which satisfy your conditions to the new array, and return the new array:

一个简单的解决方案是创建一个新数组,遍历输入数组并仅将满足条件的项添加到新数组中,然后返回新数组:

List<Car> available = new List<Car>();
foreach (Car c in cars) {
    if (c.IsAvailable) {
        available.add(c);
    }
}
//Here you can either just return the list, or create an array from it.

回答by Jim Wallace

var available = from c in cars where c.IsAvailable == true select c;

Or

或者

var available = cars.Where(c => c.IsAvailable == true);

回答by reta seboka meskele

an array is filter array when it meets the following conditions:

满足以下条件的数组为过滤器数组:

  1. if 9 exists in the list 13 must also exist
  2. if 7 exists in the list then 11 must not exist
  1. 如果列表中存在 9 则 13 也必须存在
  2. 如果 7 存在于列表中,则 11 一定不存在

solution

解决方案

int[] a = {7 , 72, 6, 13, 9 };
int i, k = 0, l = 0, m = 0, n = 0;
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 9)
    {
        k = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 13)
    {
        l = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 7)
    {
        m = 1;
    }
}
for (i = 0; i < a.Length; i++)
{
    if (a[i] == 11)
    {
        n= 1;
    }
}
if ((k == 1 && l == 1) && (m == 1 && n == 1))
{
    Console.WriteLine("is not filter array");
}
else if (k == 1 && l!= 1)
{
    Console.WriteLine("is not filter array");
}
else if (m ==1 && n==1)
{
    Console.WriteLine("is not filter array ");
}
else
    Console.WriteLine("is filter array");
Console.WriteLine("the element of an array is:");
for (i = 0; i < a.Length; i++)
{
    Console.WriteLine(a[i]);
}

As i think this code is surely work for if you need to test an array.
reta seboka ambo universtity woliso campuse department of information TECH.!!

因为我认为如果您需要测试数组,此代码肯定适用。
reta seboka ambo 大学 woliso 校园信息技术系。!!

回答by Tim Schmelter

Surprisingly, this question lacks the most natural and efficient answer: Array.FindAll

令人惊讶的是,这个问题缺乏最自然、最有效的答案: Array.FindAll

Car[] availableCars = Array.FindAll(cars, c => c.IsAvailable);

if it was a List<Car>there is also a List.FindAll.

如果是一个,List<Car>那么也有一个List.FindAll