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

发布于 2024-11-28 19:06:01 字数 1508 浏览 4 评论 0原文

我有两个向量2列表: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。

=================================================== ============================= 编辑: 有用!对于寻找如何做到这一点的人们,这是我的 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.
        }
     }

I have two Lists of vector2: Position and Floor and I'm trying to do this:
if a Position is the same as a Floor then delete the position from the List.

Here is what I thought would work but it doesn't:

    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];
        }

    }

I know this is not the good way, so how can I wright it? I need to delete all the Positions Vector2 that are the same As any of the Floors Vector2.

===============================================================================
EDIT:
It works! For people searching how to do it, here is my final code of the answer of 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.
        }
     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-12-05 19:06:01

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

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

You could do a LINQ except. This will remove everything from the Positions collection that is not in the Floor collections.

result = block.Positions.Except(block.Floor)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文