Results 1 to 9 of 9
  1. #1
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default How to check whether two elements are available in an array?

    For example,

    String str1="2"
    String str2="3"
    String [] str3={"2","4","8","9","3"};


    Is there any feature available by default to check whether both str1 and str2 are contained in str3?

    Or we can only compare by doing equals() ?

    Please give me any suggestions?

  2. #2
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    There are lots of functions in String Class
    IndexOf comes to mind at the moment

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    If move copy/move/wrap the data from an array into one of the collections classes, you could use some of their methods such as contains().
    Otherwise write a loop and use equals against each element.

  4. #4
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    I will try collection classes, if I find it difficult to handle, I am planning to use only loops as you said..
    thanks...

  5. #5
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    Any other suggestions....?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you sort the array, you could use Arrays.binarySearch(...)
    You can find it here.

  7. #7
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    thanks....
    Last edited by venkatteshb; 08-27-2008 at 09:23 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    note: cross-posted in the sun java forums.

  9. #9
    venkatteshb is offline Member
    Join Date
    Aug 2008
    Posts
    16
    Rep Power
    0

    Default

    sorry about that.....
    that will be my final cross post.....

Similar Threads

  1. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:15 PM
  2. Replies: 1
    Last Post: 01-10-2008, 01:18 AM
  3. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 AM
  4. Replies: 1
    Last Post: 12-14-2007, 07:25 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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