C#如何合并数组中的相同类型的对象

时间:2018-08-29 10:21:55

标签: c# arrays xamarin.forms

我有一个对象数组。 我想创建诸如“从A到B右移” 之类的数组语句。 但是对于同一方向,我想创建一个单一的语句,例如“从J到M左移” 我得到了所有相同类型的指导,但是无法按照我的要求合并到单个语句中。 如何合并数组中的相同内容并创建新的指令数组? 我有如下数组结构:

[  
   {  
      "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);

                }
            }
        }

2 个答案:

答案 0 :(得分:1)

让我们看看我是否正确 我们有指示 从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;
                }
            }
        }

希望这会有所帮助

答案 1 :(得分:0)

有些说明不清楚。

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""
   }
]";
}