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

メインループは配列が異なる間に繰り返します。私はすべてのゼロで同じ長さの新しいものを初期化しました。もう1つは対象のものです。

EDIT:私は配列の各位置でカスタム制限値を反復しようとしています。各サイクルで配列を使用して、与えられた数の除数で区切られた配列をロードします。私は、素因数を持つ配列と各要素の力を持つ配列をメソッドに提供します。

900の場合、素因数は[2,3,5]、電力は[2,2,2]になります。この特定のケースは、すべての要素が2であるためベース3の反復でなければなりません。これはすべてのケースで可能ではないため、より柔軟な処理をしようとしています。

私は配列の次の位置に移動するときに来るので、数値から得た値を適切にループするための変数の使い方についてのヘルプを探しています。

+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

それは完璧です。お手伝いありがとう。 –

関連する問題