从列表中删除与其他列表中任何其他对象具有相同值的所有对象。 XNA
我有两个向量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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以做一个 LINQ 除了。这将从 Positions 集合中删除 Floor 集合中不存在的所有内容。
You could do a LINQ except. This will remove everything from the Positions collection that is not in the Floor collections.