String Arrays and Equality

J

jbj

How can I validate that one string array is equal to another? I was
hoping to get a quick one line of code result, but alas, it is not
working. I tried both equality operators and their hashcodes:

Code:
_____________________________________________
String one1[] = {"hi","you"};
String one3[] = {"hi","you"};


System.out.println(one1.hashCode());
System.out.println(one3.hashCode());
System.out.println(one3==one1);
______________________________________________

With this result:
______________________________________________
17237886
8187137
false
______________________________________________

The big issue with this is that I will actually eventually be using a
double array ( a matrix if you will) of strings, so even more code
would be used. Let me know if there is a quick way to get around
this.

j
 
S

Sam

How can I validate that one string array is equal to another? I was
hoping to get a quick one line of code result, but alas, it is not
working. I tried both equality operators and their hashcodes:

Code:
_____________________________________________
String one1[] = {"hi","you"};
String one3[] = {"hi","you"};


System.out.println(one1.hashCode());
System.out.println(one3.hashCode());
System.out.println(one3==one1);
______________________________________________

With this result:
______________________________________________
17237886
8187137
false
______________________________________________

The big issue with this is that I will actually eventually be using a
double array ( a matrix if you will) of strings, so even more code
would be used. Let me know if there is a quick way to get around
this.

j

I guess the Arrays are separate objects, even if they point to Strings
which are equal. Maybe iterating through the arrays and comparing the
String instances as you go along would do the trick.

Sam90
 
T

Thomas Schodt

jbj said:
The big issue with this is that I will actually eventually be using a [two dimensional array]
so even more code would be used.
Let me know if there is a quick way to get around this.

Presumably you just want the source to be compact?

The nature of equality comparisons is such that the arrays must be
compared element by element until a pair is found that are not equal.

So "quick" can only apply to coding.
Almost sounds like you want APL rather than Java.

If you are talking large 2D arrays maybe you should be focusing on
processing time rather than coding time.
 
J

jbj

------------------------------------------------------------------------
Thomas Schodt said:
Presumably you just want the source to be compact?

The nature of equality comparisons is such that the arrays must be
compared element by element until a pair is found that are not equal.

If you are talking large 2D arrays maybe you should be focusing on
processing time rather than coding time.
-----------------------------------------------------------------------

Yes, I am talking code size (appearence, maintenence, etc). And the
arrays will never be that large (maybe 200x10 at max) so i can not
that concerned about processor cycles or O(n). Just a quick, easy way
to do a comparision (the idea is if the arrays are the same, then the
rest of my program will be skipped [which will be true 99% of the
time], so the extra overhead is acceptable in this case.

Am gonna check out the Array.equals suggestion from above, keep them
coming if anyone has an idea.

j
 
R

Roedy Green

Yes, I am talking code size (appearence, maintenence, etc). And the
arrays will never be that large (maybe 200x10 at max) so i can not
that concerned about processor cycles or O(n). Just a quick, easy way
to do a comparision (the idea is if the arrays are the same, then the
rest of my program will be skipped [which will be true 99% of the
time], so the extra overhead is acceptable in this case.

/**
* like Arrays.equals, compare two byte arrays.
*
* @param a first array
* @param b second array

* @return true if arrays have equal elements.
*/
public static boolean equals ( byte[] a, byte[] b )
{
if ( a == null || b == null )
{
return false;
}
if ( a.length != b.length )
{
return false;
}
int length = a.length;
for ( int i=0; i<length; i++ )
{
if ( a != b )
{
return false;
}
}
return true;
}
 
T

Tony Morris

How can I validate that one string [sic] array is equal to another?

@see java.util.Arrays#equals
 
T

Tony Morris

/**
* like Arrays.equals, compare two byte arrays.
*
* @param a first array
* @param b second array

* @return true if arrays have equal elements.
*/
public static boolean equals ( byte[] a, byte[] b )
{
if ( a == null || b == null )
{
return false;
}
if ( a.length != b.length )
{
return false;
}
int length = a.length;
for ( int i=0; i<length; i++ )
{
if ( a != b )
{
return false;
}
}
return true;
}


Not quite correct.
You'll find the following prints true:
System.out.println(Arrays.equals((byte[])null, (byte[])null)); // prints
true

Therefore:
if(a == null && b == null)
{
return true;
}

if(a == null || b == null)
{
return false;
}

....
 
R

Roedy Green

if(a == null && b == null)
{
return true;
}

Even better,

if ( a == b )
{
return true;
}

at the very start

Then you also optimise for the case where a and b point to the same
object.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top