【问题标题】:Assign a list of values to a scaleable custom list of objects将值列表分配给可缩放的自定义对象列表
【发布时间】:2016-12-06 20:21:00
【问题描述】:

以下是在我的列表中可以找到的最大对象集的示例,该列表是在其他地方生成的。每组中可能有更少的组或更少的值。

CustomObject COOLING_111; //Start of Cooling group 1 - section 1
CustomObject COOLING_112;
CustomObject COOLING_113;
CustomObject COOLING_114;
CustomObject COOLING_115;
CustomObject COOLING_116;
CustomObject COOLING_117;
CustomObject COOLING_118;


CustomObject COOLING_121; //Start of Cooling group 1 - section 2
...
CustomObject COOLING_128


CustomObject COOLING_211; //Start of Cooling group 2 - section 1
...
CustomObject COOLING_218;


CustomObject COOLING_221; //Start of Cooling group 2 - section 2
...
CustomObject COOLING_228;


CustomObject COOLING_311; //Start of Cooling group 3 - section 1
...
CustomObject COOLING_318;


CustomObject COOLING_321; //Start of Cooling group 3 - section 2
...
CustomObject COOLING_328;


CustomObject COOLING_411; //Start of Cooling group 4 - section 1
...
CustomObject COOLING_418;


CustomObject COOLING_421; //Start of Cooling group 4 - section 2
...
CustomObject  COOLING_428;

我如何编辑/创建一个循环或条件语句序列,以便我的数组中的变量按照示例描述的顺序专门分配:

  • 将每个对象的值设置为 -1。
  • 按以下模式设置任意数量的对象(例如前 6 个)的值:
    • 每组的第一个对象(第 1 节)
    • 然后是每组的第一个对象(第 2 节)
    • 然后回到每组的第二个对象(第 1 部分)
    • 最后是每组的第二个对象(第 2 节)
    • 例如111 -> 211 -> 311 -> 411 -> 121 -> 221 -> 321 -> 421 -> 112 -> ... -> 122 -> 等等

目前,我正在创建一个值数组,无论 CustomObjects 冷却列表的大小如何,它们都应按分配顺序进行分配。冷却列表中的对象是无序的,只能通过解析名称中的索引来区分。如果示例大小中的数组实际上是 6,那么您将在按照上面的示例分配 221 后停止。

int count = 0;
Boolean init1 = false;
Boolean init2 = false;
Boolean init3 = false;
Boolean init4 = false;
values = new int[6] {12, 18, 9, 56, 112, 187} //Simplified but normally some code is abstracted and this array comes from another part of my code

do{
  foreach (CustomObject obj in objList) {
    obj.value = -1;
    if(count < values.Length) {
      string name1 = obj.Name.substring(8);
      if (name1.StartWith("1")) {
        if (!init1) {
          obj.Value = values[count++];
          init1 = true;
        }
      }
      if (name1.StartsWith("2")) {
        if (!init2) {
          obj.Value = values[count++];
          init2 = true;
        }
      }
      if (name1.StartsWith("3")) {
        if (!init3) {
          obj.Value = values[count++];
          init3 = true;
        }
      }
      if (name1.StartsWith("4")) {
        if (!init4) {
          obj.Value = values[count++];
          init4 = true;
          break;
        }
      }
      if ((count % 4 == 0) && (count > 0) && (count < values.Length)) {
        init1 = false;
        init2 = false;
        init3 = false;
        init4 = false;
      }
      if (count == values.Length) {
        break;
      }
    }
  }
}while (count < values.Length);

【问题讨论】:

  • 您的单一问题是什么?我们不做跟随类型的问题。我们回答不仅是为了为您服务,而且最重要的是帮助未来的访客。只需为您遇到并需要帮助的一个问题创建一个minimal reproducible example
  • @rene 我如何创建一个循环或条件语句序列,以便我的数组中的变量按照示例描述的顺序专门分配:111 -> 211 -> 311 -> 411 - > 121 -> 221 -> 321 -> 421 -> 112 -> ... -> 122 -> 等等。
  • @rene 我最大的问题是我无法理解如何有效地验证我是否应该在每次进行 foreach 迭代时从我的数组中分配下一个值,同时避免分配所有的值一次到第一部分中的前 6 个(例如)元素。创建更多标志显然不是一个好的解决方案。

标签: c# loops variable-assignment


【解决方案1】:

如果您使用的名称具有合理的结构,您可以使用 Dictionary 来存储自定义对象的简单键,然后在值上使用枚举器将它们分配给自定义对象中的 Value

var dict = objList.ToDictionary( k => k.Name, v => v);
dict.Dump();

var values = new int[6] {12, 18, 9, 56, 112, 187};
// enumerator that keeps track where we are
var valuesEnumerator = values.GetEnumerator();

// set all to -1
foreach(var v in dict.Values) v.Value =-1;

const int scale = 4;
//group
for(int g = 1;  g <= scale ; g++)
{
   // section
   for(int s = 1;  s <= scale; s++)
   {
      //item
      for(int i = 1; i <= scale; i++)
      {
            // build a key
            var key = String.Format("{0}{1}{2}",i,s,g);
            // check if that key exist
            if (dict.Keys.Contains(key))
            {
                // as long as there numbers in the values array
                if (valuesEnumerator.MoveNext()) 
                {
                    // assign that value
                    dict[key].Value = (int) valuesEnumerator.Current;
                }
            }
      }
   }
}

在我的测试运行中返回:

]1

【讨论】:

  • 检查当前对象索引的枚举很好,但我真的很难复制像我的示例这样的模式。为了澄清,我该如何修改它,以便分配如下:111=12、211=18、311=9、411=56、121=112、221=187。如果我的列表中有更多值,接下来将设置 321 和 421,然后设置 112...
  • 否则,只要将 111 设置为 12, 121 = 18, 211=9, 221=56, 311=112, 321=87 等。如果有某种形式的最终变量集来修改此模式是搜索 2 组冷却对象还是 4 组(即 100 和 200 中的变量),那就太好了区分 300 和 400)而不是它们的对应部分。
  • 这里改变key的顺序:var key = String.Format("{0}{1}{2}",i,s,g);
  • 到目前为止一切顺利,但正如我所说,列表的大小可能会更小。如果一个组/部分缺少一个对象(为简单起见,我们选择 121)但我们仍然希望在对象组之间平均分配值,我们将如何做到这一点?目前,121 键不存在,我们希望将值分配给 122 并继续处理数组,就好像什么都没发生一样。如果处理得当,并且存在大量变量,则下一个循环不应覆盖 122,而是应前进到 123,而其他部分不会丢失任何对象。
  • 这听起来更像是您想将字典进一步拆分为这些组、部分、项目,然后应用逻辑。或者使用您的自定义逻辑将枚举器中的所有 for 循环折叠到下一个索引。但我已经回答了你的问题。现在由您决定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多