Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What is the best way to search for a set of values within a multi-dimenstional array?

Reply
Thread Tools

What is the best way to search for a set of values within a multi-dimenstional array?

 
 
Bill
Guest
Posts: n/a
 
      06-10-2004
suppose I have a table with 3 columns: A, B, C, and has the following
values:

A B C
1 2 3
4 5 6
7 8 9

I want to know what is the best way to find whether there is a row in
a table that contains 3 different values regardless of the orders they
are entered. For example, if I enter the search values (5,6,4) or
(4,5,6) or (4,6,5), or (5,4,6), the search should return true (row 2).

Any suggestions?
 
Reply With Quote
 
 
 
 
Andy Fish
Guest
Posts: n/a
 
      06-10-2004
"Bill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> suppose I have a table with 3 columns: A, B, C, and has the following
> values:
>
> A B C
> 1 2 3
> 4 5 6
> 7 8 9
>
> I want to know what is the best way to find whether there is a row in
> a table that contains 3 different values regardless of the orders they
> are entered. For example, if I enter the search values (5,6,4) or
> (4,5,6) or (4,6,5), or (5,4,6), the search should return true (row 2).
>
> Any suggestions?


keep a sorted copy of each row, then you can sort the input criteria and
match it up against each row by checking for equality

If you keep the values as Integer objects rather than primitive 'int's, you
can have an ArrayList for each row, then use collections.sort to sort each
individual row. Keep them all in a hashset then you can use the contains()
method to see if you have a match.

not very efficient in memory use, but saves you writing your own sort and
search logic.

Andy



 
Reply With Quote
 
 
 
 
Jean Charbonneau
Guest
Posts: n/a
 
      06-10-2004

"Bill" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> suppose I have a table with 3 columns: A, B, C, and has the following
> values:
>
> A B C
> 1 2 3
> 4 5 6
> 7 8 9
>
> I want to know what is the best way to find whether there is a row in
> a table that contains 3 different values regardless of the orders they
> are entered. For example, if I enter the search values (5,6,4) or
> (4,5,6) or (4,6,5), or (5,4,6), the search should return true (row 2).
>
> Any suggestions?


I'd loop on first column and check if there is a corresponding value, then
keep all the rows which contained one, and repeat same operation with second
row, etc.. Use a quicksort for the search ( by doing a temporary array )

Sounds good to me. Could be wrong Just giving ideas in case you need
some.


 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      06-18-2004
Andy:

It works perfectly. Thanks.

Bill

"Andy Fish" <(E-Mail Removed)> wrote in message news:<Po0yc.855$(E-Mail Removed)>...
> "Bill" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > suppose I have a table with 3 columns: A, B, C, and has the following
> > values:
> >
> > A B C
> > 1 2 3
> > 4 5 6
> > 7 8 9
> >
> > I want to know what is the best way to find whether there is a row in
> > a table that contains 3 different values regardless of the orders they
> > are entered. For example, if I enter the search values (5,6,4) or
> > (4,5,6) or (4,6,5), or (5,4,6), the search should return true (row 2).
> >
> > Any suggestions?

>
> keep a sorted copy of each row, then you can sort the input criteria and
> match it up against each row by checking for equality
>
> If you keep the values as Integer objects rather than primitive 'int's, you
> can have an ArrayList for each row, then use collections.sort to sort each
> individual row. Keep them all in a hashset then you can use the contains()
> method to see if you have a match.
>
> not very efficient in memory use, but saves you writing your own sort and
> search logic.
>
> Andy

 
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
Search regular expression with search for hex values in files? Peter Hanke Perl Misc 1 01-06-2008 08:54 PM
New Way to Search the Information - Ogleo.com - An Integrated Search Shaine Python 0 01-28-2007 08:54 AM
Using values within arrays within a hash to gsub into an input word... Abe Ruby 2 04-15-2006 08:16 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM



Advertisments