搜索列表数组 [英] Search Through List Array

查看:66
本文介绍了搜索列表数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表数组[k](多个列表).

I have a array of lists lists[k] (multiple lists).

我想搜索列表[k]中的变量PointF是否等于列表中的任何元素,除了该变量PointF所来自的列表.

I want to search if a variable PointF from a list[k] is equals to any of the elements of the lists EXCEPT the list where that variable PointF comes from.

除了搜索k之外,还对列表k + 1,k + 2,k + n和k-1,k-2,k-n进行搜索.

Something like search on lists k+1,k+2,k+n and k-1,k-2,k-n except k.

for (int k = 0; k < drone.Length; k++) 
{
    for (int i = 0; i <= 10; i++) 
    {
        for (int j = 0; j <= 10; j++) 
        {
            list[k].Add(new PointF(i,j));
        }
    }

    // now I want to search if a variable PointF 
    // from a list[k] is equals to any of the elements 
    // of the lists except the list where that variable PointF comes from
}

推荐答案

如果您知道k(您的观点来源列表的索引),则可以使用以下linq实现来实现:

If you know k (the index of the list where your point came from) you can do this with this little linq implementation:

private bool PointExistsInAnotherList(List<PointF>[] lists, int k, PointF p)
{
    return Enumerable.Range(0, lists.Length)
                     .Where(i => i != k)
                     .Any(i => lists[i].Any(point => point.Equals(p)));

}

这会检查除lists[k]之外的所有列表,如果该列表中的任何点等于给定点p.如果找到一个匹配点,它将立即返回true.

This checks for all lists except lists[k] if any of the points in that list equals the given point p. If one matching point is found, it returns true immediatly.

如果您想知道包含匹配点的列表的索引,可以执行以下操作:

If you want to know the index of the list that contains the matching point, you can do this:

private int PointExistsInAnotherList(List<PointF>[] lists, int k, PointF p)
{
    for (int i=0; i<lists.Length; i++)
    {
        if (i == k) continue;
        if (lists[i].Any(point => point.Equals(p)) return i;                                  
    }

    return -1;
}

这将返回第一个列表的索引(i),该索引包含等于p的点;如果找不到匹配的点,则返回-1.

This returns the index (i) of the first list that contains a point equal to p, or -1 if no matching point was found.

这篇关于搜索列表数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆