-2

Hi i am getting the data and i am iterating the loops using for loops.

I am having 2 arrays like

A = [{"id":1, "name":ramu},{"id":2, "name": noran},{"id":3, "name":shane}]

B = [{"id":3, "name":shane}]

I have iterated this loops like

for(userObj A : obj) {
System.out.println(A.id);
}

for(userObj B : obj) {
System.out.println(B.id);
}

Now i want to get result of common object as in the first loop i am having some extra information

res = [{"id":3, "name":shane}]

Any help in java?

  • Does this answer your question? [Java, find intersection of two arrays](https://stackoverflow.com/questions/17863319/java-find-intersection-of-two-arrays) – rph Jun 04 '20 at 16:20

1 Answers1

0

I made some slight modifications to your code since I'm not a Java expert. However it worked for me like this

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String[] A = {"ramu", "noran", "shane"};

    String[] B = {"shane"};

    for (String name : A) {
        System.out.println(name);
        String nameNeeded = name;


        for(String secondName : B) {
            System.out.println(secondName);
            String comparer = secondName;

            if(nameNeeded.equals(comparer) == true ){
                System.out.println("Match found");
            }
            else{
                System.out.println("No match");
                System.out.println("");
            }
        }
    }

All i did was compare at the end of the first for loop with an if statement and it will repeat automatically if the resulting names are not equal. Hope this helps :)

Stephen Zahra
  • 29
  • 1
  • 2