【问题标题】:Remove all the objects from a list that have the same value as any other Object from an other List. XNA从列表中删除与其他列表中的任何其他对象具有相同值的所有对象。 XNA
【发布时间】:2011-10-22 15:52:25
【问题描述】:

我有两个 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 相同的所有 Positions Vector2。

================================================ ================================= 编辑: 有用!对于搜索如何做的人,这是我对Hexxagonal答案的最终代码:

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.
        }
     }

【问题讨论】:

    标签: c# list vector xna boolean


    【解决方案1】:

    你可以做一个 LINQ 除了。这将从 Positions 集合中删除不在 Floor 集合中的所有内容。

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

    【讨论】:

    • 甜,它有效!我有一些未删除的垃圾,但我会找出如何处理其余的,谢谢!
    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2019-12-21
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多