0

For example, I have two arrays:

String [] arr1 ={"You", "book"};
String [] arr2 ={"Do", "You", "like","book" };

I want to check if arr1 match the arr2 in terms of the same order. If arr1 is {"book", "You"}, then arr1 does not match arr2.

Alright, I think that my code is wrong, but anyway:

for (int i = 0; i<arr1.length; i++){
  for (int j=0; j<arr2.length; j++){
    if (arr1[i] != arr2[j]){
      return null;
    }
  }
}

But when I run it, it always return null even my arr1 does match arr2.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • 2
    What do you want for `String[] arr1={"a", "b"}, arr2 = {"b", "c"};`? – MikeCAT Mar 07 '16 at 03:21
  • 1
    Pure code-writing requests are off-topic on Stack Overflow -- we expect questions here to relate to *specific* programming problems -- but we will happily help you write it yourself! Tell us [what you've tried](http://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. – Elliott Frisch Mar 07 '16 at 03:21
  • Since no effort is posted, here's a hint: you can use the arrays' indexes to compare whether the word appears in order. – jmcg Mar 07 '16 at 03:21
  • Take out the indexes of common words , if you just want for common words. And if you want all the elements of array1 should present in 2 ,then iterate on second and till you find first element of array1 and then again iterate till you find next. – Panther Mar 07 '16 at 03:30
  • I pinpointed your problem, it's your nested loop. Only use one loop to compare. If the arrays are different sizes, well then you definitely know they're not the same. I was going to post as answer, but this just got closed :/. – Bennett Yeo Mar 07 '16 at 03:59
  • public boolean isOrderedMatch(String[] arr1, String[] arr2){ for (int i=0, j=0; i – pczeus Mar 07 '16 at 04:01

3 Answers3

1

You can filter the elements in array 1 that aren't in array 2. This 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());
}
David
  • 557
  • 4
  • 15
0

Seeing as I have a feeling this is some kind of assignment, I'm only going to drop a couple of hints.

You're currently iterating both Arrays. This means that you compare every item to every item in the other list. For example:

String[] arr1 = { "a", "b" }
String[] arr2 = { "a", "b" }

Due to your two loops, you're comparing "a" (arr1[0]) with "a" and "b" (arr2[0] and arr2[1]). What you really want is a single loop, and you compare the same index in both Arrays against each other.

As an extra tip; you might finish your loop but not have looked at every element in both arrays.

Also, side note; using != for Strings isn't the best idea (see this question).

Community
  • 1
  • 1
whitfin
  • 4,539
  • 6
  • 39
  • 67
-1
var i=0;
while(arr1[i] == arr2[i]){    // or while(arr[i].equals(arr2[i]))
    code to put if where equal
    i++;
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68