循环取决于天数

时间:2015-08-20 22:35:34

标签: java loops counter

我正在进行休息调用以更新这3个字段,如果该字段不在哈希映射中,则确定要更新的字段,并且默认情况下更新“AA”

每次拨打电话时,服务器上的计数器也会增加。所以在3次运行之后,所有字段都在服务器上并且它总是更新“AA”,因为它是默认的,现在我希望计数器选择更新字段。所以流程继续如AA,BB,CC-AA,BB,CC-AA,BB,CC。怎么样?

    //on 1st update call,it updates AA and Counter is set to =1
    //2nd update call,it update BB = Counter is set to =2
    //3rd update call,it update CC = Counter is set to =3
    //4th update call,it update AA = Counter is set to =4
    //5th update call,it update AA = Counter is set to =5
    //6th update call,it update AA = Counter is set to =6

 if (fieldValues.containsKey("COUNTER")) {
            counter= fieldValues.get("COUNTER");
 }// how can we use this counter to select the next field

 HashMap<String, String> fieldValues = [{key,value},{key,value},{key,value}......]
 String [] FieldsToUpdate = ["AA","BB","CC"];
 String xxxFieldToUpdate = FieldsToUpdate[0];// so default is "AA"

 for(String Field : FieldsToUpdate){
        if(!fieldValues.containsKey(Field)){
            xxxFieldToUpdate = Field.trim();
            break;
        }
  }
  // Solved by calling a new method which reset the Iterator //

  String fieldToUpdate = "";
  counter= fieldValues.get("COUNTER");
  Iterator it = fieldsToUpdate.iterator();
  for(int i=0; i<counter; i++) {
  if(!it.hasNext())  // if you are at the end of the list reset iterator
    it = fieldsToUpdate.iterator();
    String res = it.next().toString();
    fieldToUpdate = res;
  }
  return fieldToUpdate;

2 个答案:

答案 0 :(得分:1)

可能使用不同的FOR循环,就像这样。

for(int i = 0; i < 3; i ++)
{
if(!fieldValues.containsKey(FieldsToUpdate[i])){
            xxxFieldToUpdate = FieldsToUpdate[i].trim();
            break;
        }
}

答案 1 :(得分:0)

//通过调用重置Iterator //

的新方法解决
String fieldToUpdate = "";
counter= fieldValues.get("COUNTER");
Iterator it = fieldsToUpdate.iterator();
  for(int i=0; i<counter; i++) {
   if(!it.hasNext())  // if you are at the end of the list reset iterator
     it = fieldsToUpdate.iterator();
     String res = it.next().toString();
   fieldToUpdate = res;
 }
 return fieldToUpdate;