searching elements of an array within another array

Discussion in 'Java' started by diffused, Jul 30, 2004.

  1. diffused

    diffused Guest

    Is it possible to search using each element in a array, to see if it
    exists in another array?

    I have two string arrays, one is hardcoded, the other one is parsed
    from a XML file.

    String[] arrayStrField = {"ClinicId","ClinicName",
    "KmRangePostcode","Suburb","State","ClinicPostcode", "Doc",
    "DateLastChanged", "AdditionalPostcodes", "ClinicStatus",
    "ClinicURL"};


    pXmlMsg.mArrField = pXmlField.getElement(i).asString();


    How would i search element by element in the mArrField to see if each
    element exists as one of the elements in the arrayStrField?

    ie ..

    boolean activeField = checkFieldArray(arrayStrField,
    xmlMess.mArrField);

    private boolean checkFieldArray(String[] arrayStrCheck, String[]
    arrayStrVar)
    {
    boolean b = false;

    // if (all arrStrVar do exist in arrayStrCheck[]
    {
    b = true;
    }


    return b;
    }


    how would i go about doing this?
    diffused, Jul 30, 2004
    #1
    1. Advertising

  2. diffused

    Jacob Guest

    diffused wrote:
    > Is it possible to search using each element in a array, to see if it
    > exists in another array?


    Arrays.asList (array1).containsAll (Arrays.asList (array2));
    Jacob, Jul 30, 2004
    #2
    1. Advertising

  3. diffused

    Oscar kind Guest

    In comp.lang.java.help diffused <> wrote:
    > Is it possible to search using each element in a array, to see if it
    > exists in another array?


    Yes, but you may want to explore the Collections framework. The reason is
    that Set's are better suited to test if it contains a certain element: For
    a properly implemented Set, this operation is O(1) instead of O(n) for a
    List/array.

    Assuming you already have two arrays of String objects, requestedColumns
    and availableColumns, you could implement something like this:

    private boolean isSubArray(String[] subArray, String[] array)
    {
    Set reference = new HastSet(Arrays.asList(subArray));

    // Test if any element of subArray is not in array.

    for (int i=0; i<subArray.length(); i++)
    {
    if (!reference.contains(subArray))
    {
    return false;
    }
    }

    // No element of subArray isn't present in array, so return true.

    return true;
    }


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jul 31, 2004
    #3
  4. diffused

    Anony! Guest


    > > Is it possible to search using each element in a array, to see if it
    > > exists in another array?

    >
    > Yes, but you may want to explore the Collections framework. The reason is
    > that Set's are better suited to test if it contains a certain element: For
    > a properly implemented Set, this operation is O(1) instead of O(n) for a
    > List/array.
    >
    > Assuming you already have two arrays of String objects, requestedColumns
    > and availableColumns, you could implement something like this:
    >
    > private boolean isSubArray(String[] subArray, String[] array)
    > {
    > Set reference = new HastSet(Arrays.asList(subArray));
    >
    > // Test if any element of subArray is not in array.
    >
    > for (int i=0; i<subArray.length(); i++)
    > {
    > if (!reference.contains(subArray))
    > {
    > return false;
    > }
    > }
    >
    > // No element of subArray isn't present in array, so return true.
    >
    > return true;
    > }


    That code won't work. You did not use the subArray argument at all in your
    isSubArray(...) method.

    You might want to fix that.

    AaA
    Anony!, Jul 31, 2004
    #4
  5. diffused

    Cid Guest

    On Sat, 31 Jul 2004 12:15:32 GMT, "Anony!" <>
    wrote:

    >
    >That code won't work. You did not use the subArray argument at all in your
    >isSubArray(...) method.
    >
    >You might want to fix that.
    >


    You might want to read it more carefully.
    Cid, Jul 31, 2004
    #5
  6. diffused

    Cid Guest

    On Sat, 31 Jul 2004 12:27:31 -0400, Cid <> wrote:

    >On Sat, 31 Jul 2004 12:15:32 GMT, "Anony!" <>
    >wrote:
    >
    >>
    >>That code won't work. You did not use the subArray argument at all in your
    >>isSubArray(...) method.
    >>
    >>You might want to fix that.
    >>

    >
    >You might want to read it more carefully.


    Or I might... Looks like the intent was to use 'array' in the loop
    structure.
    Cid, Jul 31, 2004
    #6
  7. diffused

    Oscar kind Guest

    In comp.lang.java.help Anony! <> wrote:
    >> private boolean isSubArray(String[] subArray, String[] array)
    >> {
    >> Set reference = new HastSet(Arrays.asList(subArray));


    Ah, here I made a mistake: it should read:
    Set reference = new HastSet(Arrays.asList(array));

    >>
    >> // Test if any element of subArray is not in array.
    >>
    >> for (int i=0; i<subArray.length(); i++)


    Omit the braces after "length"; it is not a method but a (read-only)
    member variable.

    >> {
    >> if (!reference.contains(subArray))
    >> {
    >> return false;
    >> }
    >> }
    >>
    >> // No element of subArray isn't present in array, so return true.
    >>
    >> return true;
    >> }

    >
    > That code won't work. You did not use the subArray argument at all in your
    > isSubArray(...) method.


    You mean the "array" parameter, right? "subArray" is used 3 times (should
    be two).

    > You might want to fix that.


    Yes, hereby the two bugs are fixed.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jul 31, 2004
    #7
  8. On Sat, 31 Jul 2004 12:15:32 +0000, Anony! wrote:

    > That code won't work. You did not use the subArray argument at all in your
    > isSubArray(...) method.
    >
    > You might want to fix that.


    You may wish to read the Oscar's code again:

    private boolean isSubArray(String[] subArray, String[] array) {
    Set reference = new HastSet(Arrays.asList(subArray));

    // Test if any element of subArray is not in array.

    for (int i=0; i<subArray.length(); i++) {
    if (!reference.contains(subArray)) {
    return false;
    }
    }
    // No element of subArray isn't present in array, so return true.

    return true;
    }

    La'ie Techie
    =?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=, Aug 1, 2004
    #8
  9. diffused

    Anony! Guest

    "Oscar kind" <> wrote in message
    news:...
    > In comp.lang.java.help Anony! <> wrote:
    > >> private boolean isSubArray(String[] subArray, String[] array)
    > >> {
    > >> Set reference = new HastSet(Arrays.asList(subArray));

    >
    > Ah, here I made a mistake: it should read:
    > Set reference = new HastSet(Arrays.asList(array));
    >
    > >>
    > >> // Test if any element of subArray is not in array.
    > >>
    > >> for (int i=0; i<subArray.length(); i++)

    >
    > Omit the braces after "length"; it is not a method but a (read-only)
    > member variable.
    >
    > >> {
    > >> if (!reference.contains(subArray))
    > >> {
    > >> return false;
    > >> }
    > >> }
    > >>
    > >> // No element of subArray isn't present in array, so return true.
    > >>
    > >> return true;
    > >> }

    > >
    > > That code won't work. You did not use the subArray argument at all in

    your
    > > isSubArray(...) method.

    >
    > You mean the "array" parameter, right? "subArray" is used 3 times (should
    > be two).
    >
    > > You might want to fix that.

    >
    > Yes, hereby the two bugs are fixed.


    - Another bug is that

    Set reference = new HastSet(Arrays.asList(array));

    should read

    Set reference = new HashSet(Arrays.asList(array));

    - I'm not sure why you need to pass two parameters to this method.

    private boolean isSubArray(String[] subArray, String[] array)

    Wouldn't it make sense to pass a single array and test if each element in
    the array is also an array?


    AaA




    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.726 / Virus Database: 481 - Release Date: 22/07/2004
    Anony!, Aug 1, 2004
    #9
  10. diffused

    Oscar kind Guest

    In comp.lang.java.help Anony! <> wrote:
    > - I'm not sure why you need to pass two parameters to this method.
    >
    > private boolean isSubArray(String[] subArray, String[] array)
    >
    > Wouldn't it make sense to pass a single array and test if each element in
    > the array is also an array?


    The OP's question was about columns of a database table: he wants to see
    if a selection of columns can be retrieved, by checking if the selected
    columns are present in the list of available columns. The second parameter
    contains the columns that exist.

    So yes, both parameters are nescessary, unless one of them happens to be
    an instance variable of the object that defines the method.


    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Aug 1, 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. Jyrki Keisala

    Elements within elements

    Jyrki Keisala, Jun 8, 2005, in forum: XML
    Replies:
    5
    Views:
    427
    William Park
    Jun 15, 2005
  2. Replies:
    10
    Views:
    675
  3. Adam Hartshorne
    Replies:
    2
    Views:
    372
    Nitin Motgi
    Jan 27, 2006
  4. Replies:
    1
    Views:
    121
  5. stumblng.tumblr
    Replies:
    1
    Views:
    198
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page