1

I got two ArrayList with Strings inside, so I want to compare those Strings with logical comparators || or && ("or" or "and") comparators and put result in a third ArrayList

Assuming

First Array contains: "Hi", "Sun", "Lamp", "pencil" Second Array contains "Sun", "chicken", "Hi"

So the result in third is suppose contains "Sun", "Hi"

2 Answers2

0
for(int i=0;i< firstlist.size();i++) {
   if(firstlist.get(i)!=null && secondlist.get(i)!=null) {
       if(firstlist.get(i).equals(secondlist.get(i))) {
          thirdlist.add(firstlist.get(i));
       }
    }
}
Deepak
  • 2,287
  • 1
  • 23
  • 30
0

The && and || operators are logical operators, i.e. they take 2 boolean expressions and return either true or false. The comparison of Strings is done using the equals() method from the String class. E.g. "hi".equals("world") returns false.

It looks like what you need to do is to iterate through the first arrayList and check if elements in it are contained in the second arrayList using the equals() method.