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. Advertisements

  2. diffused

    Jacob Guest

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

  3. diffused

    Oscar kind Guest

    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, Jul 31, 2004
    #3
  4. diffused

    Anony! Guest



    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

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

    Cid Guest

    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

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

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


    You mean the "array" parameter, right? "subArray" is used 3 times (should
    be two).
    Yes, hereby the two bugs are fixed.
     
    Oscar kind, Jul 31, 2004
    #7
  8. diffused

    Guest Guest

    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
     
    Guest, Aug 1, 2004
    #8
  9. diffused

    Anony! Guest



    - 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
     
    Anony!, Aug 1, 2004
    #9
  10. diffused

    Oscar kind Guest

    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, Aug 1, 2004
    #10
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.