C# 将两个空间对象合并到单个空间

C# 将两个空间对象合并到单个空间,c#,.net,list,merge,C#,.net,List,Merge,在这里,Space是一个以(xposition、yposition、zposition、length、depth、height)为元素的类,并且有一个类型Space的列表 我需要在列表中检查它是否遵循一些处于if条件的条件 如果它满足,那么我将两个空间合并为一个空间。之后,我删除了我使用的两个空格。它实际上意味着将两个空间合并成一个空间 将创建新列表。我再次将其视为新列表,并执行相同的过程,直到它不满足条件 我的问题是,它将进入无限循环。我想解决这个问题 public class MergeSp

在这里,Space是一个以(xposition、yposition、zposition、length、depth、height)为元素的类,并且有一个类型Space的列表

我需要在列表中检查它是否遵循一些处于if条件的条件

如果它满足,那么我将两个空间合并为一个空间。之后,我删除了我使用的两个空格。它实际上意味着将两个空间合并成一个空间

将创建新列表。我再次将其视为新列表,并执行相同的过程,直到它不满足条件

我的问题是,它将进入无限循环。我想解决这个问题

public class MergeSpace
{
    public List<Space> Mergespace(List<Space> Listofspaces)
    {
        foreach (Space space1 in Listofspaces)
        {
            foreach (Space space2 in Listofspaces)
            {
                //int count = 0;
                if ((space1.sheight == space2.sheight)
                    && (space1.sdepth == space2.sdepth)
                    && (space2.xposition == space1.xposition + space1.slength)
                    && (space2.yposition == space1.yposition)
                    && (space2.zposition == space1.zposition)
                    && (space1.semptyspace == true)
                    && (space2.semptyspace == true))
                {
                    Space space = new Space();
                    space.xposition = space1.xposition;
                    space.yposition = space1.yposition;
                    space.zposition = space1.zposition;
                    space1.slength = space1.slength + space2.slength;
                    space.sheight = space1.sheight;
                    space.sdepth = space1.sdepth;
                    space.semptyspace = true;
                    Listofspaces.Add(space);
                    Listofspaces.Remove(space1);
                    Listofspaces.Remove(space2);
                    Mergespace(Listofspaces);
                }
公共类合并空间
{
公共列表合并空间(列表合并空间)
{
foreach(Listofspaces中的空格1)
{
foreach(列表空间中的空格2)
{
//整数计数=0;
if((space1.sheight==space2.sheight)
&&(space1.sdepth==space2.sdepth)
&&(space2.xposition==space1.xposition+space1.slength)
&&(space2.yposition==space1.yposition)
&&(space2.zposition==space1.zposition)
&&(space1.semptyspace==true)
&&(space2.semptyspace==true))
{
空格=新空格();
space.xposition=space1.xposition;
space.yposition=space1.yposition;
space.zposition=space1.zposition;
space1.SLENGHT=space1.SLENGHT+space2.SLENGHT;
space.sheight=space1.sheight;
space.sdepth=space1.sdepth;
space.semptyspace=true;
添加(空格);
删除(空格1);
删除(空格2);
合并空间(列表空间);
}

对于相同的空间实例,您的条件始终满足。该实例将与其自身合并

if (!space1.Equals(space2))
{
   if ((space1.sheight == space2.sheight)
   ...
}
公共类合并空间
{
公共列表合并空间(列表合并空间)
{
List mergedspacelist=新列表();
整数计数=0;
foreach(Listofspaces中的空格1)
{
foreach(列表空间中的空格2)
{
//整数计数=0;
if((space1.sheight==space2.sheight)
&&(space1.sdepth==space2.sdepth)
&&(space2.xposition==space1.xposition+space1.slength)
&&(space2.yposition==space1.yposition)
&&(space2.zposition==space1.zposition)
&&(space1.semptyspace==true)
&&(space2.semptyspace==true))
{
空格=新空格();
space.xposition=space1.xposition;
space.yposition=space1.yposition;
space.zposition=space1.zposition;
space1.SLENGHT=space1.SLENGHT+space2.SLENGHT;
space.sheight=space1.sheight;
space.sdepth=space1.sdepth;
space.semptyspace=true;
mergedspacelist.Add(空格);
计数++;
}
}
}
如果(计数>0)
{
Mergespace(MergedSpace列表);
}
}

我不知道您的实际需要是什么,我想这将避免无限循环

No.space2.xposition==space1.xposition+space1.slengt此条件将不满足。如果space1.slengt等于0?谢谢您的回复。我可以知道还有其他比此更好的方法吗。我认为如果您签出foreac会更好如果有合并操作,请再次调用您的方法,如果没有,请不要再次调用。Listofspaces.Remove(space1);这肯定会引发错误,因为您试图在列表使用时从列表中删除项目。感谢您的回复。请您建议我如何做得更好。您找到解决问题的方法了吗?
public class MergeSpace
        {
            public List<Space> Mergespace(List<Space> Listofspaces)
            {
              List<Space> mergedspacelist = new List<Space>();
              int count=0;
                foreach (Space space1 in Listofspaces)
                {
                    foreach (Space space2 in Listofspaces)
                    {
                        //int count = 0;
                        if ((space1.sheight == space2.sheight)
                            && (space1.sdepth == space2.sdepth)
                            && (space2.xposition == space1.xposition + space1.slength)
                            && (space2.yposition == space1.yposition)
                            && (space2.zposition == space1.zposition)
                            && (space1.semptyspace == true)
                            && (space2.semptyspace == true))
                        {
                            Space space = new Space();
                           space.xposition = space1.xposition;
                            space.yposition = space1.yposition;
                            space.zposition = space1.zposition;
                            space1.slength = space1.slength + space2.slength;
                            space.sheight = space1.sheight;
                            space.sdepth = space1.sdepth;
                            space.semptyspace = true;
                            mergedspacelist .Add(space);   
                            count++;                         

                        }
                }
          }
          if(count>0)
         {
           Mergespace(mergedspacelist );
         }
}