C#如何在数组中合并相同类型的对象
我有一个对象数组。 我想创建数组语句,如“从A向右转到B” 但是对于同一个方向,我想创建一个语句,如“从J向左到M” 我得到了所有相同类型的方向,但无法按要求合并到单个语句中。 如何合并数组中的相同内容并创建新的指令数组? 我的数组结构如下:C#如何在数组中合并相同类型的对象,c#,arrays,xamarin.forms,C#,Arrays,Xamarin.forms,我有一个对象数组。 我想创建数组语句,如“从A向右转到B” 但是对于同一个方向,我想创建一个语句,如“从J向左到M” 我得到了所有相同类型的方向,但无法按要求合并到单个语句中。 如何合并数组中的相同内容并创建新的指令数组? 我的数组结构如下: [ { "label":"A", "direction":"right" }, { "label":"B", "direction":"right" }, {
[
{
"label":"A",
"direction":"right"
},
{
"label":"B",
"direction":"right"
},
{
"label":"C",
"direction":"left"
},
{
"label":"D",
"direction":"slight right"
},
{
"label":"E",
"direction":"slight right"
},
{
"label":"F",
"direction":"left"
},
{
"label":"G",
"direction":"back"
},
{
"label":"H",
"direction":"slight right"
},
{
"label":"I",
"direction":"slight right"
},
{
"label":"J",
"direction":"left"
},
{
"label":"K",
"direction":"left"
},
{
"label":"L",
"direction":"left"
},
{
"label":"M",
"direction":"left"
},
{
"label":"N",
"direction":"straight"
},
{
"label":"O",
"direction":"straight"
},
{
"label":"P",
"direction":"straight"
}
]
我的代码在这里:
var temp = null;
for (int i = 0; i < array.Count; i++)
{
if (i < array.Count - 1)
{
var start = array[i];
var next = array[i + 1];
if (!(start.direction.Equals(next.direction)))
{
string instruction = $"Go {direction} from {start.label} towards {next.label}";
System.Diagnostics.Debug.WriteLine($"Go {direction} from {start.label} towards {next.label}");
instructionList.Add(instruction);
//Assign new next node to temp
temp = next;
}
else
{
string instruction = $"Go {direction} from {temp.label} towards {next.label}";
System.Diagnostics.Debug.WriteLine($"Go {direction} from {temp.label} towards {next.label}");
//Same direction start and end label
//Here how to merge the same instruction into single
instructionList.add(instruction);
}
}
}
var-temp=null;
for(int i=0;i
让我们看看我是否做对了
我们有指示
从A->B向右转
从B->C向右转
现在我们要将它们合并到
从A->C向右走
for循环似乎很好。但在它里面,逻辑似乎并不完全正确。
如果当前指令和下一条指令的方向相同,我们不希望
做任何事。临时变量已经将指令存储在该方向第一次指向的位置。
如果当前指令和下一条指令有不同的方向,我们希望添加一条从临时变量到下一条指令的当前方向的指令。然后将临时变量设置为下一条指令
要确保临时变量在第一个方向交换时不为null,请将其设置为for循环之前的第一条指令,且不为null
var temp = array[0];
for (int i = 0; i < array.Count; i++)
{
if (i < array.Count - 1)
{
var start = array[i];
var next = array[i + 1];
if (!(start.direction.Equals(next.direction)))
{
string instruction = $"Go {direction} from {temp.label} towards {next.label}";
instructionList.Add(instruction);
//Assign new next node to temp
temp = next;
}
}
}
var-temp=array[0];
for(int i=0;i
希望这能有所帮助有些说明不清楚
void Main()
{
Directions[] directions = JsonConvert.DeserializeObject<Directions[]>(GetJson());
List<Instruction> instructions = new List<Instruction>();
var direction = directions[0];
for (int i = 0; i < directions.Length; i++)
{
if (directions[i].Direction != direction.Direction)
{
instructions.Add(new Instruction { StartPoint = direction.Label, EndPoint = directions[i - 1].Label, Direction = direction.Direction });
direction = directions[i];
}
}
instructions.Add(new Instruction { StartPoint = direction.Label, EndPoint = directions[directions.Length - 1].Label, Direction = direction.Direction });
foreach (var i in instructions)
{
Console.WriteLine($"Go {i.Direction} from {i.StartPoint} to {i.EndPoint}");
}
}
public class Instruction
{
public string StartPoint { get; set; }
public string EndPoint { get; set; }
public string Direction { get; set; }
}
public class Directions
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("direction")]
public string Direction { get; set; }
}
private string GetJson()
{
return @"
[
{
""label"":""A"",
""direction"":""right""
},
{
""label"":""B"",
""direction"":""right""
},
{
""label"":""C"",
""direction"":""left""
},
{
""label"":""D"",
""direction"":""slight right""
},
{
""label"":""E"",
""direction"":""slight right""
},
{
""label"":""F"",
""direction"":""left""
},
{
""label"":""G"",
""direction"":""back""
},
{
""label"":""H"",
""direction"":""slight right""
},
{
""label"":""I"",
""direction"":""slight right""
},
{
""label"":""J"",
""direction"":""left""
},
{
""label"":""K"",
""direction"":""left""
},
{
""label"":""L"",
""direction"":""left""
},
{
""label"":""M"",
""direction"":""left""
},
{
""label"":""N"",
""direction"":""straight""
},
{
""label"":""O"",
""direction"":""straight""
},
{
""label"":""P"",
""direction"":""straight""
}
]";
}
void Main()
{
方向[]方向=JsonConvert.DeserializeObject(GetJson());
列表说明=新列表();
变量方向=方向[0];
for(int i=0;i
使用Linq
以分组方式获得结果(即,右、左等分别分组),然后对特定分组执行所需操作。