Comparing Arrays – Java Programming Tutorial 18

In this video we will see how we can compare two arrays to see if two arrays are exactly the same thing so in this case you can see I’ve declared two arrays numbers 1 and numbers 2 and you can see that they’ve got exactly the same values at the same positions okay so in this case we could go.

And say if numbers 1 is the same as numbers 2 and then print out if they are the same we’re gonna send we’re gonna print out they are the same and if they’re not the same we’re gonna go to the else and we print out they.

Are not the same okay so let’s look at the logic of.

This one quickly if we print it out and we run it.

Now I’m just let’s just run this one while still running the the previous example so if I run this one now you can see they say it says that they are not the same so.

The thing here is you can see the two arrays are exactly the same it’s 2 4 6 8 10 but when we test using the double equal sign operator there it will say there are not the same so remember the same as with strings if you try to compare objects with a double quotation or sorry a double equal sign then.

It means that you’re comparing the memory locations of these two objects and that’s why you cannot compare using the double equal sign so essentially in memory this object gets created some way.
And the memory location of it will be stored in two.

Numbers one this one gets created some way and the memory location of it will be stored in numbers too but where they get created.

Is two different places although the values they have is exactly the same way so that’s why we cannot compare using a double equal sign date because the double equal sign is comparing whether they’re memory locations or exactly the same thing so if I do this now if I say that one is equal to numbers 1 then it.

Means that I’m essentially copying the memory location of numbers 1 into numbers 2 so if I run this now it will say they are the same so why are they now the same because I’ve copied the memory location of the two ok so numbers 1 will point to this and numbers two will point to know what number one is pointing and that’s also the exact same object so I’m pointing both now.

Numbers 1 and numbers 2 it is now essentially pointing to the exact same object so if I change something in numbers 2 or change something in numbers 1 it will go and make the change do the same array just go back and get.

Back to that value right so they we’ve got the two arrays again so this is the wrong way of doing it you cannot compare two arrays by using the double equal sign so how then do.

We compare two arrays so in order to do this I’m gonna use a boolean variable and I’m gonna start this or call it arrays equal and let’s set this one to true so we’re gonna assume that the two are true unless we can prove otherwise and we’ll set the value to false and I’m also gonna need a counter they or variable my loop control variable when we’re gonna use later on to taste for the values I guess I’ll need two variables.

Arrays equal and then an index value to basically check every value inside of.

The arrays right so step number one is let’s test if the two arrays are of the same size because if this one is can only store three values and that one can store.