2017-04-06 7 views
-1

我正在嘗試使用自定義值填充給定另一個數組的數組。舉例來說,如果我的基地是[2,3,1]數組應該是這樣的:當我到達[2,3,1]在Java中使用ArrayList開發自定義計數

0,0,0 
0,0,1 
0,1,0 
0,1,1 
0,2,0 
0,2,1 
0,3,0 
0,3,1 
1,0,0 and so on... 

循環應該結束。我對代碼進行了一些更改,因此可能會比以前更糟。

max=0; 
while (!(power.containsAll(listPot))){ 
     while ((index<=listPot.get(pos))){ //Iterating the first digit 
      power.set(pos, index); //Updating the value 
      index++; //Increasing up to the limit found on listPot 
     } 
     index=0; //Reset the counter to put into the Array 
     if(max<limit){ //max would be the position of the next digit 
      max++; 
     } 
     if(power.get(max)<listPot.get(pos)){ //If next digit is < limit 
      index++; //move the index forward 
      power.set(max, index); //Increase the next digit 
      while (pos>=0){ //Removing the previous digits, like from 199 to 200 
       power.set(pos, 0); 
       pos--; 
      } 
      index=0; 
     } 
     else { //I increase max position again and proceed like above 
      pos=max; 
      max++; 
      while (pos>=0){ //Removing previous digits 
       power.set(pos, 0); 
       pos--; 
      } 
      index++; //Increasing the value to input 
      power.set(max, index); 
     } 
     index=0; //Reset the counters 
     pos=0; 
    } 

主循環在數組不同時迭代。我初始化了全長爲零的新長度。另一個是目標之一。

編輯:我想在數組的每個位置迭代自定義限制的值。我將在每個循環中使用數組來加載一個給定數字的除數的分離數組。我向該方法提供了一個具有主要因素的數組,另一個使用了每個因子的權力。

對於900,結果是素數因子爲[2,3,5],功率爲[2,2,2]。這個特殊情況應該是基3迭代,因爲所有元素都是2.這對所有情況都是不可能的,所以我試圖做更靈活的事情。

當我移動到數組的下一個位置時,我的問題就出現了,所以我正在尋求幫助,以便使用變量正確循環來自數字的值。

+2

你能告訴我們你想達到什麼目的嗎?你的問題很難理解 –

+1

那麼,你的問題是什麼? –

+0

我不明白:你想要構建的數組是什麼? –

回答

0

據我所知,你想增加數組中的值達到給定的限制,這是一些排列組合。我建議來劃分,如問題:

calculate(new int[] {2, 3, 1}); // example 


private static void calculate(int[] limits) { 
    int counters[] = new int[limits.length]; 
    do { 
     something(counters); 
    } while (increment(counters, limits)); 
} 

private static void something(int[] counters) { 
    // do whatever should be done with the counters 
    System.out.println(Arrays.toString(counters)); 
} 

private static boolean increment(int[] counters, int[] limits) { 
    // index for counter to increment, starting at the last one 
    for (int i = counters.length-1; i >= 0; i--) { 
     counters[i] += 1; 

     // if the counter is within the limits, return true 
     if (counters[i] <= limits[i]) 
      return true; 

     // if counted past limit, reset to zero and continue 
     // with previous counter (loop) 
     counters[i] = 0; 
    } 

    // if the first counter exceeded the limit, we are done 
    return false; 
} 

其中calculate的入口點/主迴路,something是做什麼應該做的每個交互(這裏只是打印),並increment遞增排列,返回false如果在結束

+0

這很完美。感謝您的幫助。 –