How to compare 2 int arrays?

  • Thread starter yaya via DotNetMonster.com
  • Start date
Y

yaya via DotNetMonster.com

Hi, is that any way to compare 2 int arrays without comparing each of the elements ?

int []p = new int[3] {1,2,3};
int []q = new int[3] {1,2,3};

if(p == q)
MessageBox.Show(" Arrays are equal! ");

But the (p==q) return false while the arrays p and q have the same elements.

Thankz,
yaya
 
M

Morten Wennevik

Hi yaya,

As far as I know you need to compare individual elements in a loop to
determine.

public bool ArrayComparison(int[] a, int[] b)
{
if(a == null || b == null)
return false;
if(a.Length != b.Length)
return false;

for(int i = 0; i < a.Length; i++)
if(a != b)
return false;

return true;
}

Hi, is that any way to compare 2 int arrays without comparing each of
the elements ?

int []p = new int[3] {1,2,3};
int []q = new int[3] {1,2,3};

if(p == q)
MessageBox.Show(" Arrays are equal! ");

But the (p==q) return false while the arrays p and q have the same
elements.

Thankz,
yaya
 
U

Uchiha Jax

I know you could do something with operator overloading and change the way
in which the syntax would recognise the statement if(p == q) it would mean
you'd probably have to write the equivalent of a method that takes two
integer arrays and returns true if they're equal or false if they're not but
the code should be relatively simple to write.
Alternatively you could just write a method that does this rather than
operator overloading.
 
Y

yaya via DotNetMonster.com

The '==' only works when comparing the arrays with same reference object, "(q == r)" will return true when...

int []p = new int[3] {1,2,3};
int []q = new int[3];
int []r = new int[3];

q = p;
r = p;

As a result, I have to compare each elements of the 2 arrays, thankz anyway...
 
G

Guest

The '==' only works when comparing the arrays with same reference object,
"(q == r)" will return true when...
int []p = new int[3] {1,2,3};
int []q = new int[3];
int []r = new int[3];

q = p;
r = p;

Here's a minor point, but might be interesting. The second and third "=new
int[]" are not needed here.

p, q, r and references. You set q and r to newly allocated arrays, and then
immediately throw those arrays away by setting q and r to refer to the first
array, so the following code does the same thing and doesn't waste time
allocating arrays that are not needed:

int[] p = new int[3] {1,2,3};
int[] q = p;
int[] r = p;

Mark
 
G

Guest

Uchiha Jax said:
I know you could do something with operator overloading and change the way
in which the syntax would recognise the statement if(p == q)

Unfortunately, you can't do it with operator overloading. Overloading
operators need to go inside a class or struct and one of the parameters must
be of the enclosing type. In other words, you would need to add the operator
to the "int[]" type.

Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top