私は、条件に基づいて反復的に削除された場合、ArrayListの内容を更新する方法を見つけようとしています。私が持っているのは、特定の方法で問題を解決するためにユーザーが選択した方法(スイッチケース経由)を使用するアルゴリズムです。しかし、一定量の反復の後に改善が見出されない場合、それはランダムに異なる利用可能な方法を選択する(場合)。繰り返しごとにArraylistの内容を更新する
ゴール: 方法が選択されている場合は、再び利用できなくなります。
コード:
public Solution solve() throws IOException, EclipseException {
// Account for all the available switch cases
ArrayList<Integer> selection = new ArrayList<Integer>();
for(int y = 1; y < 12; y++) {
selection.add(new Integer(1*y));
}
ArrayList<Integer> listToKeep = new ArrayList<Integer>();
for (int i = 0; i < iterations; i++) {
// Iterative process with a counter for each iteration that provides
// no improvement
if (counter == 6) {
for (Integer num : selection) {
if (num != method){
listToKeep.add(num);
}
}
selection.clear();
selection.addAll(listToKeep);
System.out.println("Sent for selection " + selection);
Random rand = new Random();
method = selection.get(rand.nextInt(selection.size()));
System.out.println("New randomly selected method is: " + method);
solve(method);
}
}
return bestSolution;
}
所望の結果:
All cases: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Initital method chosen: 1
Sent for selection [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
New randomly selected method is: 8
Sent for selection [2, 3, 4, 5, 6, 7, 9, 10, 11]
New randomly selected method is: 9
etc.
問題: ザ・ループのために更新するのではなく、(すべての数値を含む元のArrayListの選択を参照し続けますonに基づいてlistToKeep)、最後に選択された大文字のみを削除します。
質問: 選択リストが反復ごとに正しく更新されることをどうすれば確認できますか?
フィードバックや代替方法は大変ありがとうございます。
あなたが同時変更の場合にIterator
を使用することができ18-06
if (!alreadyExecuted){
selection = IntStream.range(1, 12).boxed().collect(Collectors.toList());
Collections.shuffle(selection);
}
alreadyExecuted = true;
int newMethod = selection.get(selection.size() - 1);
selection.remove(selection.size() - 1);
あなたの非常に賢い選択肢のJanosに感謝します。私が正しく理解していれば、1)私の新しいメソッドは、シャッフルされたリストの最後の要素と同じです。 2)その後、最後の要素が削除されます。 –
@ArjenPetersが正しいとし、最後の要素を削除するか、インデックスを使用して選択する次の要素を追跡します。 – janos
@Jonas Roger!私はちょっと考えて遊ぼうと思う。メソッド= selection.get(selection.size() - 1); selection.remove(selection.size() - 1);リストは同じ長さのままです。 –