# Thread: logic flaw in comparison?

1. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## logic flaw in comparison?

Does this follow a typical argument structure, or is my for loop just not correct?

public static boolean areEqual (int[] a1, int[] a2)
{

boolean equalArrays = true;

for( int i = 0; i < a1.length; i++)
{

if (
(

(a1 == null && a2 == null)

&

( a1.length == a2.length)

&

(a1[i] == a2[i])
)
)
return equalArrays;
else return false;
}
return boolean;
}

}  Reply With Quote

2. Senior Member Join Date
May 2010
Posts
436
Rep Power
12

##  Originally Posted by Adomini Does this follow a typical argument structure, or is my for loop just not correct?
Please clarify this statement as it doesn't make sense to me. Also, you'll want to edit your post above so that code tags are used above and below your posted code so that it will retain its formatting (the forum FAQ will show you how). If the code is easier to read, more will read it.  Reply With Quote

3. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## Right. I am trying to use a Boolean argument in comparing two int [] arrays. Using a Boolean argument, logically, is what i was trying to do. If two arrays are both null or they have the same length and have the same elements, then they are the same.

Java Code:
```public static boolean areEqual (int[] a1, int[] a2)
{

boolean equalArrays = true;

for( int i = 0; i < a1.length; i++)
{

if (
(

(a1 == null && a2 == null)

&

( a1.length == a2.length)

&

(a1[i] == a2[i])
)
)
return equalArrays;
else return false;
}
return boolean;
}

}```  Reply With Quote

4. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## after the null comparison, I just switched an 'or' (|) sign instead of the 'and' (&) sign, to make more sense.  Reply With Quote

5. Senior Member Join Date
May 2010
Posts
436
Rep Power
12

## There's something wrong with your code indentation, and it's still quite hard to read.

You want to ask yourself a few questions:
1) Do you want to check if the arrays are null once before the for loop or do you want to do this inside the for loop with each iteration of the loop?
2) Same for each array's length -- do you want to check it once before the for loop or do you want to check with each iteration of the for loop?

You'll want to think through your code logically, stepping into it walking through it mentally just as if you were the computer.  Reply With Quote

6. Senior Member Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14

## Hi,

First you should know the difference between the "&&" logic operator and the "&" bitwise operator.

true && true = true
true && false = false

0000 & 0000 = 0000
1010 & 1000 = 1000  Reply With Quote

7. Senior Member Join Date
May 2010
Posts
436
Rep Power
12

##  Originally Posted by wsaryada Hi,

First you should know the difference between the "&&" logic operator and the "&" bitwise operator.

true && true = true
true && false = false

0000 & 0000 = 0000
1010 & 1000 = 1000

wsaryada: While & is a bitwise operator, you do know that & is also a boolean operator just like && the difference being && will short circuit if the first test is false while & won't. So you understand that this is perfectly valid, right?:

Java Code:
```true & true = true
true & false = false```
Point number 2: what the heck does this have to do with the original poster's question or problem?
Last edited by curmudgeon; 08-31-2010 at 04:57 AM.  Reply With Quote

8. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## Thanks for the response guys. What I though I could do was have everything under the for loop and have it say
1) if both are null, they are equal
2) if they are the same length AND have the same elements, then they are equal.

Otherwise, they are false.

i will check the indention and see if that is what is causing the problem. I guess I only need the for loop for the length comparison, but doing that looks kinda funny to me.

Only reason I had '&' and not "&&" is because a similar expression in the Java book did it, so it just happened to work too.  Reply With Quote

9. ## As noted above, will the length change as you loop through the for loop? Or is the length of each array the same throughout? If it doesn't change, why check it inside the loop. I think that the only thing that should be checked in the loop is to see if the i'th item from one array is the same as the i'th item from the other array, that's it. And you're not going to want to even do the loop if either array is null or the lengths are different.  Reply With Quote

10. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## the length will change as we go through the loop. This is supposed to be a blank formula/ code that can take the length of any arrays and compare them.
I am having a small issue with the return false statment at the end however.

I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

Java Code:
```public static boolean areEqual (int[] a1, int[] a2)
{  // why is this giving me an error? That belongs there!

boolean equalArrays = true;

if ( (a1 == null && a2 == null))
return equalArrays;

if ( a1.length == a2.length)
return equalArrays;

for( int i = 0; i < a1.length; i++ )
{

if (a1[i] == a2[i])

return equalArrays;
}

else return false;

}```  Reply With Quote

11. ##  Originally Posted by Adomini the length will change as we go through the loop.
How? The length of an array never changes once it is set. Yes, this method should take arrays of different lengths, and yes, each time the method is called the arrays passed into the parameters may have different lengths from before, but each time the method is called, within the method code itself the length of both arrays do not change throughout the method; again, they simply can't. Period. End of story. Finito.

I am having a small issue with the return false statment at the end however.

I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

Java Code:
```public static boolean areEqual (int[] a1, int[] a2)
{  // why is this giving me an error? That belongs there! [color="red"]what is the error?[/color]

boolean equalArrays = true;

if ( (a1 == null && a2 == null))
return equalArrays;

if ( a1.length == a2.length)

for( int i = 0; i < a1.length; i++ )
{

if (a1[i] == a2[i])

}

else return false;

}```

The key to solving this is to write down how you would solve this on paper. If you had two lists of numbers, how would you see if both are the same? What steps would you take? And when in the process do you say, I'm done, the answer is ***? This is the equivalent of the return statement above.

For example, I'd do something like:
First check if the lists exist. If one doesn't exist, I'd complain (that's called throwing an exception in Java) and exit.
Next I'd check if the arrays were the same size. If they were different size, I'd say that the answer is that they're not equal and would exit (return).

Next I'd loop through the arrays, comparing each item at the same level or index as I looped through them, if any item is different from the other, I'd give the answer that the arrays were different and would exit.

If I'm now at the end and didn't exit, then I'd say the arrays were the same.

Do you see how this logic is different from yours above?
Luck.
Last edited by Fubarable; 08-31-2010 at 06:09 AM.  Reply With Quote

12. Senior Member Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14

##  Originally Posted by curmudgeon wsaryada: While & is a bitwise operator, you do know that & is also a boolean operator just like && the difference being && will short circuit if the first test is false while & won't. So you understand that this is perfectly valid, right?:

Java Code:
```true & true = true
true & false = false```
Point number 2: what the heck does this have to do with the original poster's question or problem?
Yes, you are correct. My bad, I didn't look at the code deeper :D  Reply With Quote

13. Senior Member Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14

##  Originally Posted by Adomini the length will change as we go through the loop. This is supposed to be a blank formula/ code that can take the length of any arrays and compare them.
I am having a small issue with the return false statment at the end however.

I am trying to get the computer to see that if none of those three true conditions meet, then the result should be false.

Java Code:
```public static boolean areEqual (int[] a1, int[] a2)
{  // why is this giving me an error? That belongs there!

boolean equalArrays = true;

if ( (a1 == null && a2 == null))
return equalArrays;

if ( a1.length == a2.length)
return equalArrays;

for( int i = 0; i < a1.length; i++ )
{

if (a1[i] == a2[i])

return equalArrays;
}

else return false;

}```
Your else-block doesn't match any corresponding if statement. Or just remove the else and says return false.
Last edited by wsaryada; 08-31-2010 at 06:21 AM.  Reply With Quote

14. Member Join Date
Aug 2010
Posts
70
Rep Power
0

## Thanks everyone. I think I got it now. Fubarable, that makes sense. It must be the late hour......... dont know why I missed it.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•