Cycle is only executed once within a while and should always be executed

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 randomly sort the ListA with list elements. It turns out that it only goes through when the element 2 is added to the listA and then it does not re-enter the for. I'm more accustomed to using vb and there the code works normally, but in android studio it gets in an infinite loop because no element is added to the listA anymore. I do not know if something was missing, so that it is executed correctly, since it is not very used to Java language.

edit: I put the int y = 0 out of for to see if it would be the y that held the value after the first execution of the, but has the same effect, only goes in for 1 time. That's why I'm not realizing why it is not running again.

    
asked by anonymous 25.04.2018 / 00:30

1 answer

0

The list depends on the number of records in the firebase, that is, it is unknown. The ListA value is incremented As a new element is randomly selected from list b, that is, whenever the new value does not exist in listA, it is added to the listA. It begins with 0 elements and the 1st element is added in the if (n) , in the 2nd and remaining loops of the while it is in that it is checked if the random number already exists in theA list, if there is back to the while to create a new random number , if it does not exist it is added to theA list.

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

    
25.04.2018 / 14:35