searching elements of an array within another array

D

diffused

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?
 
J

Jacob

diffused said:
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));
 
O

Oscar kind

In comp.lang.java.help diffused said:
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;
}
 
A

Anony!

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
 
C

Cid

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

Oscar kind

In comp.lang.java.help Anony! said:
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));

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

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.

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
 
A

Anony!

Oscar kind said:
In comp.lang.java.help Anony! said:
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));

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
 
O

Oscar kind

In comp.lang.java.help Anony! said:
- 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.
 

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top