String Arrays and Equality

Discussion in 'Java' started by jbj, Jul 14, 2004.

  1. jbj

    jbj Guest

    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
     
    jbj, Jul 14, 2004
    #1
    1. Advertising

  2. jbj

    Roedy Green Guest

    On 14 Jul 2004 13:50:10 -0700, (jbj) wrote or
    quoted :

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


    see http://mindprod.com/jgloss/gotchas.html#COMPARISON

    you must do a for loop and compare elementwise.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 14, 2004
    #2
    1. Advertising

  3. jbj

    Sam Guest

    (jbj) wrote in message news:<>...
    > 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
     
    Sam, Jul 15, 2004
    #3
  4. jbj

    Filip Larsen Guest

    "jbj" wrote

    > How can I validate that one string array is equal to another?


    The java.util.Arrays.equals methods are your friends here.


    Regards,
    --
    Filip Larsen
     
    Filip Larsen, Jul 15, 2004
    #4
  5. jbj wrote:

    > 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.
     
    Thomas Schodt, Jul 15, 2004
    #5
  6. jbj

    jbj Guest

    ------------------------------------------------------------------------
    Thomas Schodt <> wrote in message news:<cd594v$jor$1$>...
    > 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
     
    jbj, Jul 15, 2004
    #6
  7. jbj

    Roedy Green Guest

    On 15 Jul 2004 06:21:58 -0700, (jbj) wrote or
    quoted :

    >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;
    }

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 15, 2004
    #7
  8. jbj

    Tony Morris Guest

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

    @see java.util.Arrays#equals

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Jul 16, 2004
    #8
  9. jbj

    Tony Morris Guest

    > /**
    > * 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;
    }

    ....
    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Jul 16, 2004
    #9
  10. jbj

    Roedy Green Guest

    On Fri, 16 Jul 2004 04:50:33 GMT, "Tony Morris" <>
    wrote or quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 16, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sb
    Replies:
    4
    Views:
    440
    Nick Hounsome
    Apr 3, 2004
  2. sb
    Replies:
    2
    Views:
    401
    Nick Hounsome
    Apr 3, 2004
  3. Philipp
    Replies:
    21
    Views:
    1,160
    Philipp
    Jan 20, 2009
  4. Stephanie

    case sensitive / insensitive string equality

    Stephanie, Oct 3, 2005, in forum: ASP General
    Replies:
    2
    Views:
    186
    Steven Burn
    Oct 3, 2005
  5. Kannan Jay
    Replies:
    2
    Views:
    114
    Alex DeCaria
    Apr 13, 2010
Loading...

Share This Page