Java: How check if two arrays are in same order? -
this question has answer here:
- how compare strings in java? 23 answers
for example, have 2 arrays:
string [] arr1 ={"you", "book"}; string [] arr2 ={"do", "you", "like","book" }; i want check if arr1 match arr2 in terms of same order. if arr1 {"book", "you"}, arr1 not match arr2.
alright, think code wrong, anyway:
for (int = 0; i<arr1.length; i++){ (int j=0; j<arr2.length; j++){ if (arr1[i] != arr2[j]){ return null; } } } but when run it, return null arr1 match arr2.
you can filter elements in array 1 aren't in array 2. result should match array 1.
public static boolean withinandinorder(string[] a, string[] b) { return arrays.equals(a, arrays.stream(b) .parallel() .filter(element -> arrays.binarysearch(a, element) >= 0) .toarray()); }
Comments
Post a Comment