Results 1 to 6 of 6
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default Compare value in array

    How to compare value in array?

    e.g. I have Array named list which contains 12 elements.I wanted to compare the value of the array e.g. I wanted to know if index 0 equal or not equal to index 2.

    I didnt get the right code. I have tried code:

    Java Code:
    if (list.get(0)==list.get(2)
    {
        System.out.println(equal); 
    }

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    == only works for numeric values.
    use Value1.equals(value2) for strings, or else check the api Array (Java 2 Platform SE v1.4.2)
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    what list var is? Is it Vector or what
    And what type list uses?

  4. #4
    anthonym2121 is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by cassysumandak View Post
    How to compare value in array?

    e.g. I have Array named list which contains 12 elements.I wanted to compare the value of the array e.g. I wanted to know if index 0 equal or not equal to index 2.

    I didnt get the right code. I have tried code:

    Java Code:
    if (list.get(0)==list.get(2)
    {
        System.out.println(equal); 
    }
    don't you need to use square brackets[] if its an array?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If what you calling an "array" is a List (for example an instance of ArrayList), try

    Java Code:
    if (list.get(0).equals(list.get(2))
    {
        System.out.println("equal"); 
    }
    The point is that collections contain references to objects, not primitives and those references should be compared using equals(). Additionally you may want to be fussy about the case when list.get(0) is null.

  6. #6
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

Similar Threads

  1. Compare a char
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 02-01-2009, 04:24 PM
  2. How to compare Built_versions???
    By KMS in forum Advanced Java
    Replies: 1
    Last Post: 09-23-2008, 05:06 AM
  3. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  4. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 AM
  5. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •