2017-06-17 21 views
1

私は、条件に基づいて反復的に削除された場合、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); 

答えて

0

、 のようなもの、リストをシャッフルするために、よりシンプルで効率的 、常に最後の要素になります。

List<Integer> selection = IntStream.range(1, 13).boxed().collect(Collectors.toList()); 
Collections.shuffle(selection); 

このリストの最後の要素はランダムになります。 最後の要素を削除するのは安いです。 (例ではArrayListの場合は、 の後にある要素をコピーする必要があります)

+0

あなたの非常に賢い選択肢のJanosに感謝します。私が正しく理解していれば、1)私の新しいメソッドは、シャッフルされたリストの最後の要素と同じです。 2)その後、最後の要素が削除されます。 –

+0

@ArjenPetersが正しいとし、最後の要素を削除するか、インデックスを使用して選択する次の要素を追跡します。 – janos

+0

@Jonas Roger!私はちょっと考えて遊ぼうと思う。メソッド= selection.get(selection.size() - 1); selection.remove(selection.size() - 1);リストは同じ長さのままです。 –

0

ソリューション編集。代わりに、リストからランダムな要素を除去する

List<Integer> selection = new ArrayList<>(); 

for (Iterator<Integer> iterator = selection.iterator(); iterator.hasNext();) { 
    Integer val = iterator.next(); 
    if (check_for_condition) { 
     // Remove the current element from the iterator and the list. 
     iterator.remove(); 
    } 
} 
+0

ありがとうございます!以前は非常に似たイテレーターを使用していましたが、条件が真であれば要素を削除するように機能しましたが、希望の結果に記載されているように、選択の全体的な内容をいくつかの繰り返しにわたって更新しませんでした。しかし、私はそれをもう一度チェックし、あなたに戻ってきます。 –

+0

は答えがあれば受け入れます。 – Ravi

関連する問題