简体   繁体   中英

Getting Unmatched values by comparing two ArrayList

I have Compared two ArrayLists named alStringName and alNameAll

Then, added it to ArrayList named alNameSelected as below :

            for(int i=0;i<alStringName.size();i++){
                for(int j=0;j<alNameAll.size();j++){
                    if (alStringName.get(i).equals(alNameAll.get(j))) {

                        alNameSelected.add(alStringName.get(i));

                    }
                }
            }

Now, Its working fine. But, I have to add unmatched content or data to another ArrayList named alNameUnknown

How can I do that ?

Replace your inner loop with a 'match or not' check:

for(int j=0;j<alNameAll.size();j++){
                    if (alStringName.get(i).equals(alNameAll.get(j))) {

                        alNameSelected.add(alStringName.get(i));

                    }
                }

becomes:

if ( alNameAll.contains(alStringName.get(i)){
  alNameSelected.add(alStringName.get(i));
}else{
  alNameUnknown.add(alStringName.get(i));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM