I have the following problem that I tried to solve, but I cannot come with a solution:
Write in a class a method that receives as parameters two arrays of numbers a and b and returns true if the elements of b are found in a in the order of their appearance and false otherwise.
If the first string has length 0 the method returns false. If the second string is 0, the method returns true.
The signature of the method is: public static boolean contains(int[] a, int[] b)
Ex:
contains({1, 2, 3, 4, 2, 1, 3, 4}, {2, 3, 4}) -> true
contains({1, 2, 3, 4, 2, 1, 3, 4}, {1, 4, 3}) -> true
contains({1, 2, 3, 4, 2, 1, 3, 4}, {3, 3, 2}) -> false
contains({1, 2, 3, 4, 2, 1, 3, 4}, {1, 1, 2}) -> false
contains({1, 2, 3, 4, 2, 1, 3, 4}, {}) -> true
contains({}, {1, 2, 3}) -> false
My solution until now:
public class TestEA8 {
public static boolean contains(int[] a, int[] b) {
int i=0;
int j=0;
boolean result = false;
if(a.length == 0){
System.out.println(false);
return false;
}else if(b.length ==0){
System.out.println(true);
return true;}
for (i=0;i<b.length;i++){
for (j=0;j<a.length;j++){
if (b[i] == a[j]){
result = true;
break;
}
}
if(j==a.length){
result =false;
}
}
System.out.println(result);
return result;
}
}
Thank you,
bis a subsequence ofa.Collection.containsAll(): docs.oracle.com/javase/8/docs/api/java/util/…