winnergo
winnergo

Reputation: 101

Array comparison for equality

I've checked and noticed there are several other threads concerning this topic, however I didn't find any that would directly aim at this specific question, neither did I see answers to it. So here is my code :

    public static boolean equals(int[] array1, int[] array2)
    {
    boolean trueFalse = true;
    while(array1.length == array2.length)
    {
        int i = 0;
        int j = 0;
        if(array1[i] == array2[j])
        {
            i++;
            j++;
            return trueFalse;
        }
        else
        {
            trueFalse = false;
            break;
        }
    }
    return trueFalse;

I have 2 arrays :

    int[] array = {2, 30, 22, 1, 5};
    int[] array1 = {2, 30, 4, 1, 5};

Although they are clearly not equal the program returns true. I am aware of the inbuilt function Arrays.equals(), however I want to do it manually in order to understand the function in its entirety.

Upvotes: 0

Views: 681

Answers (5)

khelwood
khelwood

Reputation: 59240

  1. If the arrays are of different lengths, then they are not equal, so return immediately.
  2. Use a for loop to iterate through the arrays.
  3. Only return true after the whole loop is finished and no difference has been found.
public static boolean equals(int[] array1, int[] array2) {
    if (array1.length != array2.length) {
        return false;
    }
    for (int i = 0; i < array1.length; ++i) {
        if (array1[i] != array2[i]) {
            return false;
        }
    }
    return true;
}

The Arrays.equals implementation is pretty similar to this. It also null-checks both arrays to avoid throwing a NullPointerException.

Upvotes: 7

Ahmad Mekheber
Ahmad Mekheber

Reputation: 21

You shouldn't call return when (array1[i] == array2[j])

also while will loop forever

this is how the code should be:

public static boolean equals(int[] array1, int[] array2)
{

if(array1.length != array2.length) return false;

int i = 0;
int j = 0;
while(i < array1.length)  //or i < array2.length
{

    if(array1[i] == array2[j])
    {
        i++;
        j++;
    }
    else
    {
        return false;
    }
}
return true;

Upvotes: 0

geco17
geco17

Reputation: 5294

Try something like this. Set your control variable (I've renamed it to equal) to true and if anything fails (arrays aren't the same size or an element isn't equal) then set it to false, break and return.

Note that you only need one index since the arrays have to be the same size and an array, to be equal, must have the same elements in the same order.

Also, somewhat a matter of opinion, but having only one return statement can be beneficial for readability and maintenance.

public static boolean equals(int[] array1, int[] array2) {
boolean equal = true;
if (array1.length == array2.length) {
    for (int i = 0; i < array1.length; i++) {
        if (array1[i] != array2[i]) {
            equal = false;
            break;
        }
    }
} else {
    equal = false;
}
return equal;

Upvotes: 0

Elarbi Mohamed Aymen
Elarbi Mohamed Aymen

Reputation: 1710

your problem is that the while loop will work only once same for the if() statement, to compare two arrays you can use the code below:

public static boolean equals(int[] array1, int[] array2) {
    if (array1.length != array2.length) {
        return false;
    } else {
        for (int i = 0; i < array1.length; i++) {
            if (array1[i] != array2[i]) {
                return false;
            }
        }
        return true;
    }
}

Upvotes: 0

mdolata
mdolata

Reputation: 184

Try this :

boolean check(int[] arr1, int[] arr2){
    if (arr1.length != arr2.length) return false;

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

Upvotes: 0

Related Questions