196k views
4 votes
Write a method that accepts two string arrays and print common values between the two arrays (Perform case sensitive search while finding common values).

1 Answer

1 vote

Answer:

public class Main

{

public static void main(String[] args) {

String[] s1 = {"Hi", "there", ",", "this", "is", "Java!"};

String[] s2 = {".", "hi", "Java!", "this", "it"};

findCommonValues(s1, s2);

}

public static void findCommonValues(String[] s1, String[] s2){

for (String x : s1){

for (String y : s2){

if(x.equals(y))

System.out.println(x);

}

}

}

}

Step-by-step explanation:

*The code is in Java.

Create a method named findCommonValues that takes two parameters, s1 and s2

Inside the method:

Create a nested for-each loop. The outer loop iterates through the s1 and inner loop iterates through the s2. Check if a value in s1, x, equals to value in s2, y, using equals() method. If such a value is found, print that value

Inside the main method:

Initialize two string arrays

Call the findCommonValues() method passing those arrays as parameters

User LaTeXFan
by
7.7k points