从列表中删除与其他列表中任何其他对象具有相同值的所有对象.XNA

本文关键字:其他 对象 列表 XNA 任何 删除 | 更新日期: 2023-09-27 18:04:45

我有两个列表vector2: Position和Floor,我想这样做:如果位置与下限相同,则从列表中删除该位置。

这是我认为会工作,但它没有:

    public void GenerateFloor()
    {
        //I didn't past all, the code add vectors to the floor List, etc.
        block.Floor.Add(new Vector2(block.Texture.Width, block.Texture.Height) + RoomLocation);
        // And here is the way I thought to delete the positions:
        block.Positions.RemoveAll(FloorSafe);
    }
    private bool FloorSafe(Vector2 x)
    {
        foreach (Vector2 j in block.Floor)
        {
            return x == j;
        }
        //or
        for (int n = 0; n < block.Floor.Count; n++)
        {
            return x == block.Floor[n];
        }
    }

我知道这不是一个好方法,那么我该怎么写呢?我需要删除所有的位置Vector2是相同的任何地板Vector2。

===============================================================================编辑:它的工作原理!对于搜索如何做到这一点的人,这里是我的答案的最终代码六边形:

public void FloorSafe()
    {
        //Gets all the Vectors that are not equal to the Positions List.
        IEnumerable<Vector2> ReversedResult = block.Positions.Except(block.Floor);
        //Gets all the Vectors that are not equal to the result..
        //(the ones that are equal to the Positions).
        IEnumerable<Vector2> Result = block.Positions.Except(ReversedResult);
        foreach (Vector2 Positions in Result.ToList())
        {
            block.Positions.Remove(Positions); //Remove all the vectors from the List.
        }
     }

从列表中删除与其他列表中任何其他对象具有相同值的所有对象.XNA

你可以做LINQ除了。这将删除position集合中不属于Floor集合的所有内容。

result = block.Positions.Except(block.Floor)