Cycle for only runs once inside a while and should always be run

Asked

Viewed 45 times

0

listaA = new ArrayList <>();
                boolean n = true;
                while (listaA.size() < listab.size()) {
                    Log.i( "while ordena" , String.valueOf( listaA.size() ));
                    valorAl = nRandom.nextInt( listab.size() );
                    Log.i( "while ordena" , "   valor random  " + String.valueOf( valorAl ));
                    if (valorAl == listab.size()) {
                        valorAl = valorAl - 1;
                        Log.i( "while ordena" , "   valor random igual a listab.size");
                    }
                    if (n) {
                        listaA.add( listab.get( valorAl ) );
                        Log.i( "while ordena" ,"  1 valor na lista A");
                        n = false;
                    } else {
                        Log.i( "while ordena" , "  proximo valor da lista A - " + String.valueOf( listab.get( valorAl ).id ) );

                        Boolean igual =false;
//int y =0;
                        for (int y =0; y==(listaA.size()-1); y++){
                        if (listaA.get(y).id== listab.get( valorAl ).id )  {igual=true; Log.i( "while ordena" , "  já existe");}
                           if(!igual) {listaA.add( listab.get( valorAl ) );
                             Log.i( "while ordena" , "  adiciona novo");}
                        }
                    }
                }

I am using this code to sort randomly the listA with the elements of the listb. The thing is, it only goes through the for when the 2 element A is added to the listA and then it doesn’t go back into the for. I’m more used to using Vb and there the code works normally, but in android studio is in an infinite loop, because no element is added to the listA. I don’t know if I’ve missed something so that it can be executed correctly, since I’m not very used to the java language.

Edit: I put the int y=0 out of the for to see if it was the y that held the value after the first run of the for, but it has the same effect, it only goes into the for 1 time. So I don’t understand why the is not being executed again.

  • How many values does the listab?

1 answer

0

The list depends on the number of firebase records, that is, it is not known. The listThe value is incremented As a new element chosen randomly from the listb is added, that is, whenever the new value does not exist in the listA, it is added to the listA. Starts with 0 elements and is added the 1st element in if (n), in the 2nd and remaining while turns is in the is that it is checked if the random number already exists in the listA, if it exists back to the while to be created a new random number, if it does not exist is added to the listA.

Although I figured out what the problem was, it was a math mistake on my part. I used the following for (int y =0; y==(listaA.size()-1); y++) and by changing to for (int y =0; y<listaA.size(); y++) already works correctly.

Browser other questions tagged

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