vendredi 30 octobre 2020

Can`t compare ArrayList values in Java

Im doing a Java course and in one exercise I have to create three ArrayLists, ask the user to fill the first two with Integers, and then compare both ArrayLists.

The values that don´t repeat are added to the third ArrayList. I already declared the ArrayLists, used Scanner to allow the user to fill the ArrayLists, and that part works.

The problem comes when I try to compare both ArrayLists. I get all sort of alerts in this line ("the if statement is redundant", "Integer values compared using == or !=","Flip operands of the binary operator", "Invert if").

I suspect that what I wrote after the if statement is not very clean, and that I could get some comments about that (Im not an expert in Java), but I do not understand the alerts that the IDE displays. The code compiles and runs just fine until it hits the nested loops. Please help! Thanks.

       //Checking for values that dont repeat
        for(int i=0;i<listVector1.size();i++){
            for(int j=0;j<listVector2.size();i++){
                if(listVector1.get(i)==listVector2.get(j)){//Im getting an alert here
                 repeats=true; //this boolean was previously declared
                 } else {
                  repeats=false;
                }
            if(repeats==false){
                int newValue=listVector1.get(i);
                listVector3.add(newValue);
            }
        }

        }

Aucun commentaire:

Enregistrer un commentaire