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

Discussion in 'Java' started by Bill, Jun 10, 2004.

  1. Bill

    Bill Guest

    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?
     
    Bill, Jun 10, 2004
    #1
    1. Advertising

  2. Bill

    Andy Fish Guest

    "Bill" <> wrote in message
    news:...
    > 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
     
    Andy Fish, Jun 10, 2004
    #2
    1. Advertising

  3. "Bill" <> wrote in message
    news:...
    > 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.
     
    Jean Charbonneau, Jun 10, 2004
    #3
  4. Bill

    Bill Guest

    Andy:

    It works perfectly. Thanks.

    Bill

    "Andy Fish" <> wrote in message news:<Po0yc.855$>...
    > "Bill" <> wrote in message
    > news:...
    > > 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
     
    Bill, Jun 18, 2004
    #4
    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. THY
    Replies:
    3
    Views:
    11,578
    Steve C. Orr, MCSD
    Aug 18, 2003
  2. news.microsoft.com

    Best way to create multi-page "wizards'

    news.microsoft.com, May 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    686
    Warren J. Hairston
    May 15, 2004
  3. Alan Silver
    Replies:
    0
    Views:
    554
    Alan Silver
    May 30, 2006
  4. Abby Lee
    Replies:
    5
    Views:
    442
    Abby Lee
    Aug 2, 2004
  5. Daniel Webb
    Replies:
    6
    Views:
    97
    Daniel Webb
    Nov 24, 2009
Loading...

Share This Page