2016-08-01 38 views
-2

下面是可在我的列表中找到的最大对象集合的示例,这些对象可在其他位置生成。每个组中可能会有更少的组或更少的值。为可缩放的对象自定义列表指定值列表

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部分)
    • THEN各组的第一个目的(节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); 
+0

是什么你的*单*问题?我们不跟随*类型的问题。我们不仅仅为您服务,而且最重要的是帮助未来的访客。只需为一个问题创建一个[mcve],并且需要帮助。 – rene

+0

@rene如何创建一个循环或一系列条件语句,以便我的数组中的变量按以下示例中所述的顺序进行特定分配:111→211→311→411→121→221→ 321 - > 421 - > 112 - > ... - > 122 - >等 – trev

+0

@rene我最大的问题是我很难理解如何有效地验证我是否应该每次从我的数组中分配下一个值我通过foreach迭代继续前进,同时避免将所有值一次分配给第一部分中的前6个元素(例如)。创建更多的标志显然不是一个好的解决方案。 – trev

回答

1

如果您使用名称具有合理的结构,你可以使用一个Dictionary到一个简单的键存储到您的CustomObjects,然后使用一个枚举的值,将其分配给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; 
       } 
      } 
     } 
    } 
} 

在我的测试运行这个返回:

[enter image description here] 1

+0

枚举检查当前对象索引是好的,但我真的很努力复制像我的例子一样的模式。为了澄清,我该如何修改它,以便分配如下:111 = 12,211 = 18,311 = 9,411 = 56,121 = 112,221 = 187。如果在我的列表中有更多的值,则321和421将会被设置,然后是112 ... – trev

+0

否则,只要遵循模式并修改阵列的填充方式(非常具有挑战性)在111被设置为12之后,121 = 18,211 = 9,221 = 56,311 = 112,321 = 87等。如果存在某种形式的最终变量集以修改该模式搜索2一组冷却物体或4个(即在100和200之间区分300和400之间的变量)而不是它们对应的部分。 – trev

+1

它确实有效...如果测试集足够大... – rene