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.

08-31-2010, 04:31 AM

Adomini

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.

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.

08-31-2010, 04:47 AM

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

08-31-2010, 04:55 AM

curmudgeon

Quote:

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?:

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?

08-31-2010, 05:18 AM

Adomini

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.

08-31-2010, 05:21 AM

Fubarable

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.

08-31-2010, 05:48 AM

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.

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.

Quote:

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.

return equalArrays; // [color="red"]are you sure about this line?[/color]
}

else return false;

}

See comments above.

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.

08-31-2010, 06:06 AM

wsaryada

Quote:

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?:

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

08-31-2010, 06:10 AM

wsaryada

Quote:

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.