【发布时间】: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