3

Check to see if the array arr1 contain the same elements as arr2 in the same order in java.

for example:

 isTheSame({"1", "2", "3"}, {"1", "2", "3"}) → true
 isTheSame({"1", "2", "3"}, {"2", "1", "1"}) → false
 isTheSame({"1", "2", "3"}, {"3", "1", "2"}) → false

so far i have

public boolean isTheSame(String[] arr1, String[] arr2)
{
 if (arr1.length == arr2.length)
 {
 for (int i = 0; i < arr1.length; i++)
 {
 if (arr1[i] == arr2[i])
 {
 return true;
 }
 }
 }
 return false; 
 }

The problem with this is that it only compares the first element of the two arrays.

Dumbo
14.2k56 gold badges193 silver badges292 bronze badges
asked Jan 2, 2013 at 18:25
2
  • Is the closing parenthesis on if (arr1[i] == arr2[i] there in your actual program? Commented Jan 2, 2013 at 18:27
  • 2
    For starters, read this on how to compare strings in java, and fix your compile time error (you forgot to close parenthesis on second if condition). Commented Jan 2, 2013 at 18:28

3 Answers 3

10

You are iterating until you find a match. You should instead be looking for a String which doesn't match and you should be using equals not ==

// same as Arrays.equals()
public boolean isTheSame(String[] arr1, String[] arr2) {
 if (arr1.length != arr2.length) return false;
 for (int i = 0; i < arr1.length; i++)
 if (!arr1[i].equals(arr2[i]))
 return false;
 return true;
}

FYI This is what Arrays.equals does as it handle null values as well.

public static boolean equals(Object[] a, Object[] a2) {
 if (a==a2)
 return true;
 if (a==null || a2==null)
 return false;
 int length = a.length;
 if (a2.length != length)
 return false;
 for (int i=0; i<length; i++) {
 Object o1 = a[i];
 Object o2 = a2[i];
 if (!(o1==null ? o2==null : o1.equals(o2)))
 return false;
 }
 return true;
}
answered Jan 2, 2013 at 18:28

1 Comment

Solid answer. It always pays to take a look at the library implementation first.
1
public boolean isTheSame(String[] arr1, String[] arr2)
{
 if (arr1.length == arr2.length)
 {
 for (int i = 0; i < arr1.length; i++)
 {
 if ((arr1[i] != null && arr2[i] != null && !arr1[i].equals(arr2[i]))
 || (arr1[i] != null && arr2[i] == null) || 
 (arr2[i] != null && arr1[i] == null))
 {
 return false;
 }
 }
 } else {
 return false; 
 }
 return true; 
 }

But it is very unoptimal.

answered Jan 2, 2013 at 18:29

Comments

1

Yes, it will only compare the first element. Look what it is doing:

public boolean isTheSame(String[] arr1, String[] arr2)
{
 if (arr1.length == arr2.length)
 {
 for (int i = 0; i < arr1.length; i++)
 {
 // if the elements are equal ..
 // (NOTE: this will only be true for INTERNED strings!!)
 if (arr1[i] == arr2[i]) // fixed typo
 {
 // .. then STOP LOOKING and RETURN TRUE
 // wait- WHY?
 return true;
 }
 }
 // searched everything- but still RETURN FALSE??
 }
 return false; 
 }

While the usage of == is not the problem with the provided example data due to string interning, it will bite you someday for real data. Don't use == for objects unless identity equality is desired.

answered Jan 2, 2013 at 18:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.