Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > searching elements of an array within another array

Reply
Thread Tools

searching elements of an array within another array

 
 
diffused
Guest
Posts: n/a
 
      07-30-2004
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[i] = 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[i] do exist in arrayStrCheck[]
{
b = true;
}


return b;
}


how would i go about doing this?
 
Reply With Quote
 
 
 
 
Jacob
Guest
Posts: n/a
 
      07-30-2004
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));


 
Reply With Quote
 
 
 
 
Oscar kind
Guest
Posts: n/a
 
      07-31-2004
In comp.lang.java.help diffused <(E-Mail Removed)> 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[i]))
{
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
 
Reply With Quote
 
Anony!
Guest
Posts: n/a
 
      07-31-2004

> > 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[i]))
> {
> 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


 
Reply With Quote
 
Cid
Guest
Posts: n/a
 
      07-31-2004
On Sat, 31 Jul 2004 12:15:32 GMT, "Anony!" <(E-Mail Removed)>
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.
 
Reply With Quote
 
Cid
Guest
Posts: n/a
 
      07-31-2004
On Sat, 31 Jul 2004 12:27:31 -0400, Cid <(E-Mail Removed)> wrote:

>On Sat, 31 Jul 2004 12:15:32 GMT, "Anony!" <(E-Mail Removed)>
>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.
 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      07-31-2004
In comp.lang.java.help Anony! <(E-Mail Removed)> 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[i]))
>> {
>> 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
 
Reply With Quote
 
=?UTF-8?b?TMSByrtpZSBUZWNoaWU=?=
Guest
Posts: n/a
 
      07-31-2004
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[i])) {
return false;
}
}
// No element of subArray isn't present in array, so return true.

return true;
}

La'ie Techie

 
Reply With Quote
 
Anony!
Guest
Posts: n/a
 
      08-01-2004

"Oscar kind" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In comp.lang.java.help Anony! <(E-Mail Removed)> 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[i]))
> >> {
> >> 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


 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      08-01-2004
In comp.lang.java.help Anony! <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Google search result to be URL-limited when searching site, but notwhen searching Web stumblng.tumblr Javascript 1 02-04-2008 09:01 AM
Searching within the hash values of an array of hashes usenet@DavidFilmer.com Perl Misc 1 08-26-2007 04:55 AM
initialize an array of elements that contain another array wenmang@yahoo.com C Programming 10 08-02-2006 04:52 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Elements within elements Jyrki Keisala XML 5 06-15-2005 04:58 PM



Advertisments