Shuffle List

Asked

Viewed 126 times

0

I would like to shuffle an Arraylist, and I’m trying to do this with Collections.Sort(), but I don’t think it’s happening

The code I’m using is this:

Thread t = new Thread(new Runnable() {
           @Override
           public void run() {
               ImageView imageView = findViewById(R.id.imagePadrao);
               int m = 0;
               int n = 0;
               List<Integer> op = new ArrayList<Integer>();
               op.add(R.drawable.pedra);
               op.add(R.drawable.papel);
               op.add(R.drawable.tesoura);

               Collections.sort( op );

               Iterator iterator = op.iterator();
               for (int i = 0; i < 6; i++){
                   n++;
                   Log.d("Incremento", ""+n);
                   while (iterator.hasNext()){
                       m = (int) iterator.next();
                       Log.d( "Numero", ""+m );
                       imageView.setImageResource( m );
                       try {
                           Thread.sleep(100);
                       } catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
                   Collections.sort( op );
                   iterator = op.iterator();
               }
           }
       });
       t.start();

To know if it’s shuffling I’m looking at the console and forever be the same sequence

07-31 12:05:54.591 6136-6168/com.use.jokenpo D/Incremento: 1
07-31 12:05:54.591 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:54.694 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:54.797 6136-6168/com.use.jokenpo D/Numero: 2131099762
07-31 12:05:54.901 6136-6168/com.use.jokenpo D/Incremento: 2
07-31 12:05:54.901 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:55.004 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:55.107 6136-6168/com.use.jokenpo D/Numero: 2131099762
07-31 12:05:55.211 6136-6168/com.use.jokenpo D/Incremento: 3
07-31 12:05:55.211 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:55.317 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:55.419 6136-6168/com.use.jokenpo D/Numero: 2131099762
07-31 12:05:55.522 6136-6168/com.use.jokenpo D/Incremento: 4
07-31 12:05:55.522 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:55.626 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:55.729 6136-6168/com.use.jokenpo D/Numero: 2131099762
07-31 12:05:55.832 6136-6168/com.use.jokenpo D/Incremento: 5
07-31 12:05:55.832 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:55.936 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:56.040 6136-6168/com.use.jokenpo D/Numero: 2131099762
07-31 12:05:56.143 6136-6168/com.use.jokenpo D/Incremento: 6
07-31 12:05:56.143 6136-6168/com.use.jokenpo D/Numero: 2131099747
07-31 12:05:56.250 6136-6168/com.use.jokenpo D/Numero: 2131099749
07-31 12:05:56.353 6136-6168/com.use.jokenpo D/Numero: 2131099762

I wish every time I rotate the loop it would scramble again

  • Mix, as opposed to sort rsrs

  • Now yes. It worked. Thank you!

  • Can add question, Please!

1 answer

2


.sort() serves to sort a list, use .shuffle() to "shuffle".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.