&引用;序列不包含匹配元素“;C#
我正在制作一个应用程序,它具有&引用;序列不包含匹配元素“;C#,c#,algorithm,linq,C#,Algorithm,Linq,我正在制作一个应用程序,它具有容器,具有X、Y和Z属性。这些集装箱装在船上。所有容器都是正常的,冷却的或有价值的。有价值的需求是X-1和X+1是空的。因此,基本上,贵重容器旁边的容器必须是空的。当它们为空时,我希望它返回介于两者之间的容器 我用X、Y和Z预先制作了一个列表 public void ConstructShip() { for (int x = 1; x < widthX + 1; x++) { for (int y = 1; y < le
容器,具有X
、Y
和Z
属性。这些集装箱装在船上
。所有容器都是正常的,冷却的
或有价值的
。有价值的需求是X-1和X+1是空的。因此,基本上,贵重容器旁边的容器必须是空的。当它们为空时,我希望它返回介于两者之间的容器
我用X
、Y
和Z
预先制作了一个列表
public void ConstructShip()
{
for (int x = 1; x < widthX + 1; x++)
{
for (int y = 1; y < lengthY + 1; y++)
{
for (int z = 1; z < heightZ + 1; z++)
{
SortedContainers.Add(new Container(x, y, z, true, false, false, 0)); //xyz, (bool)empty, (bool)valuable, (bool)cooled, weight
}
}
}
}
澄清一下:我想要一个空的容器(列表中的所有容器都已经是空的),其中的边,因此X-1和X+1也是空的。或者,如果位置为X=1,则只有X=2需要为空且无价值,或者X=MaxXLength需要为空且无价值,或者只有X=MaxXLength-1需要为空且无价值
单元测试:
[TestInitialize()]
public void Initialize()
{
Ship ship = new Ship(4, 3, 3); //lengthX, widthY, heightZ
for (int x = 1; x < 5; x++)
{
for (int y = 1; y < 4; y++)
{
for (int z = 1; z < 4; z++)
{
ship.SortedContainers.Add(new Container(x, y, z, true, false, false, 0));
}
}
}
}
[TestMethod()]
public void CheckValuableAndReturnPlaceTest_Working()
{
Container freeContainer = ship.CheckValuableAndReturnPlace();
Container expectedContainer = new Container(1, 1, 1, true, false, false, 0);
Assert.AreEqual(JsonConvert.SerializeObject(expectedContainer), JsonConvert.SerializeObject(freeContainer));
}
[TestInitialize()]
公共无效初始化()
{
Ship Ship=新船(4,3,3);//长度x,宽度,高度z
对于(int x=1;x<5;x++)
{
对于(int y=1;y<4;y++)
{
对于(intz=1;z<4;z++)
{
ship.SortedContainers.Add(新集装箱(x,y,z,true,false,false,0));
}
}
}
}
[TestMethod()]
公共无效支票有价证券和换地证券测试工作()
{
Container freeContainer=ship.checkValuable和ReturnPlace();
Container expectedContainer=新容器(1,1,1,true,false,false,0);
AreEqual(JsonConvert.SerializeObject(expectedContainer),JsonConvert.SerializeObject(freeContainer));
}
上次编辑:
对不起,我的问题这么含糊,我尽了最大努力不提供多余的不需要的信息,但我找到了答案。谢谢一个人给我一个答案:p.我想这个问题不仅仅是标题。但是如果你的标题中提到的错误阻止了你,请看这篇文章。
如果我理解正确,您希望找到第一个容器,其中它和它的邻居都是空的,没有价值。我不太明白为什么,但这似乎就是你的帖子所描述的
您提到希望找到所有容器,其中侧面也满足这些要求,但仅参考x方向
我想要一个空的容器(列表中的所有容器都已经是空的),其中的边是空的,所以X-1和X+1也是空的
由于您正在征求有关逻辑的建议,因此我遵循的是“边”而不是逻辑(仅提及x)
如果我是你,我会依靠一点额外的封装来解决这个问题。将箱子分开分类并跟踪其位置。有一种方法是这样的
public class ContainerGrid{
public List<List<List<Container>>> Locations = new List<List<List<Container>>>();
public List<Container> OrderedList;//you can keep sorting the containers and their current locations separate but still cleanly managed this way.
public bool IsEmpty(int x, int y, int z){
//interpreting what you said about needing to be empty and not valuable to be viable.
return containers[x][y][z].empty && !containers[x][y][z].valuable;
}
public bool IsEmptyAndNeighborsEmpty(int x, int y, int z){
//i am unsure if you intended sides to be only in the x direction
//or if that was just a part of how you explained it in the question
return IsEmpty(x,y,z)
&& IsEmpty(x-1,y,z) && IsEmpty(x+1,y,z);
//&& IsEmpty(x,y-1,z) && IsEmpty(x,y+1,z);
//&& IsEmpty(x,y,z-1) && IsEmpty(x,y,z+1);
}
public bool HasLocation(int x, int y, int z){
return Locations[x]!=null && Locations[x][y] != null && Locations[x][y][z]!=null;
}
public bool Add(Container container){
if(!HasLocation(container.x,container.y,container.z)){
if(Locations[container.x]==null)
AddAt<List<List<Container>>>(Locations, container.x, new List<List<Container>>(), false);
if(Locations[container.x][container.y]==null)
AddAt<List<Container>>(Locations[container.x], container.y, new List<Container>(), false);
if(Locations[container.x][container.y][container.z]==null)
AddAt<Container>(Locations[container.x][container.y], container.z, container, true);
OrderedList.Add(container);
}else return false;
}
public bool AddAt<T>(List<T> list, int index, T value, bool nullUntil){
while(list.Count<=index+1) list.add(nullUntil ? null : value);
list[index]=value;
}
}
按您的意愿排序列表,但仍保持相同的空间关系
这能回答你的问题吗?你能澄清一下这个答案中没有包括的任何内容吗?SortedContainerStacks.Where((c,i)=>c.X>0和&c.X
。@郑荣才•格斯伯格的建议实际上似乎很好,看起来它处理边缘情况,但它总是说第一个和最后一个容器没有价值。你可能想在说明书中说明,如果你想让它们有价值,如果它们唯一的邻居是空的或不是空的。另外,您可以使用任何,而不是使用第一个默认值
,然后检查它是否为空
,这个问题我不清楚-您在标题中提到一个错误,然后您谈论“返回”容器,但给出一个无效方法,然后描述一个失败的测试,但不显示测试。我认为,花更多的精力澄清这个问题比增加一笔赏金要有用得多。恐怕还有太多我们不知道的地方。什么是过滤容器?所有这些容器构造函数参数是什么?哪个LINQ调用失败?目前,我们有一个部分代码转储,但没有多少迹象表明您在诊断问题时做了什么。我解决了它,但因为每个人都说我的问题不够清楚。我不认为我应该得到别人深思熟虑的回答,但是因为你是唯一一个给我深思熟虑的答案的人,我会给你我的观点。我会删除这篇帖子,因为我已经找到了我想要的方式。
public class ContainerGrid{
public List<List<List<Container>>> Locations = new List<List<List<Container>>>();
public List<Container> OrderedList;//you can keep sorting the containers and their current locations separate but still cleanly managed this way.
public bool IsEmpty(int x, int y, int z){
//interpreting what you said about needing to be empty and not valuable to be viable.
return containers[x][y][z].empty && !containers[x][y][z].valuable;
}
public bool IsEmptyAndNeighborsEmpty(int x, int y, int z){
//i am unsure if you intended sides to be only in the x direction
//or if that was just a part of how you explained it in the question
return IsEmpty(x,y,z)
&& IsEmpty(x-1,y,z) && IsEmpty(x+1,y,z);
//&& IsEmpty(x,y-1,z) && IsEmpty(x,y+1,z);
//&& IsEmpty(x,y,z-1) && IsEmpty(x,y,z+1);
}
public bool HasLocation(int x, int y, int z){
return Locations[x]!=null && Locations[x][y] != null && Locations[x][y][z]!=null;
}
public bool Add(Container container){
if(!HasLocation(container.x,container.y,container.z)){
if(Locations[container.x]==null)
AddAt<List<List<Container>>>(Locations, container.x, new List<List<Container>>(), false);
if(Locations[container.x][container.y]==null)
AddAt<List<Container>>(Locations[container.x], container.y, new List<Container>(), false);
if(Locations[container.x][container.y][container.z]==null)
AddAt<Container>(Locations[container.x][container.y], container.z, container, true);
OrderedList.Add(container);
}else return false;
}
public bool AddAt<T>(List<T> list, int index, T value, bool nullUntil){
while(list.Count<=index+1) list.add(nullUntil ? null : value);
list[index]=value;
}
}
public ContainerGrid ShipYard;
public Container FindFirstEmptyContainer(){
return ShipYard.OrderedList.FirstOrDefault(x=>ShipYard.IsEmptyAndNeighborsEmpty(x.x,x.y,x.z));
}