简体   繁体   中英

How to loop through an ArrayList and check if it contains values from another ArrayList in Java

So I have two ArrayLists to loop through. What I want to do is to loop through each object in the first ArrayList and check it with all the values contained in the second ArrayList. So in other words is it possible to check each object in the first ArrayList with every single value stored in the second ArrayList? I want to be able to do this for every single object in my first ArrayList. Also at the end of the loop, if theres an object in my first ArrayList that contains no values in my second ArrayList, I want print out that object(or value in that list).

StackOverflow isn't meant to be a free coding service, but I can give you guidance on how to go about it:

  1. Iterate over the elements of the first ArrayList with a for/for-each loop
  2. Check whether the second ArrayList contains the element using ArrayList.contains
  3. If the element doesn't exist in the second ArrayList, append it to some third ArrayList using ArrayList.add
  4. After your for loop, print out the elements of the third ArrayList (which contains the elements which exist in the first ArrayList but not the second)

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