1 2 Previous Next 16 Replies Latest reply: Oct 10, 2006 5:44 PM by 807607 RSS

    retrieving all possible terms from multidimensional array

    807607
      Hi,

      I have a multidimensional as such:

      String[][] stringArray:

      stringArray[0] = [white]
      stringArray[1] = [hard][soft]
      stringArray[2] = [hat][cap]

      I'm having brainfart and can't seem to iterate through this array to obtain a list that contains these strings:

      white hard hat
      white hard cap
      white soft hat
      white soft cap

      Any suggestions?
        • 1. Re: retrieving all possible terms from multidimensional array
          807607
          stringArray[0] = [white]
          Yes, initialize it like this:
          stringArray[0] = [white][white]
          :-p
          • 2. Re: retrieving all possible terms from multidimensional array
            807607
            I thought about that... but these multidimensional array are dynamically created by another method.

            The array could be:

            stringArray[0]=[white][yellow]
            stringArray[1]=[glove][hat][sock][shoe]

            need:
            white glove
            white glove
            white hat
            white sock
            yellow shoe
            yellow hat
            yellow sock
            yellow shoe

            or it could be

            stringArray[0]=[blue]
            stringArray[1]=[glove][hat][sock][shoe]
            stringArray[2]=[soft][hard]
            stringArray[3]=[short]

            need:
            blue glove soft short
            blue hat soft short
            blue sock soft short
            .
            .
            .

            Is there a more efficient way of doing this if we encounter a situation like the second array from above?

            Message was edited by:
            blazinbobo
            • 3. Re: retrieving all possible terms from multidimensional array
              807607
              > The array could be:
              >
              stringArray[0]=[white][yellow]
              stringArray[1]=[glove][hat][sock][shoe]

              need:
              > white glove
              > white glove
              white hat
              white sock
              > yellow shoe
              yellow hat
              yellow sock
              > yellow shoe


              How did you arrive at those needs from that array?

              ~
              • 4. Re: retrieving all possible terms from multidimensional array
                807607
                whoops... bad case of copy and paste:

                should have been:

                white glove
                white hat
                white sock
                white shoe
                yellow glove
                yellow hat
                yellow sock
                yellow shoe
                • 5. Re: retrieving all possible terms from multidimensional array
                  807607
                  The easiest way would be to nest 3 for loops.
                  • 6. Re: retrieving all possible terms from multidimensional array
                    807607
                    The easiest way would be to nest 3 for loops.
                    that also check for null array elements..
                    • 7. Re: retrieving all possible terms from multidimensional array
                      807607
                      On second thought, I see that your second example has two and four-dimensional arrays instead of three. If you don't know how many dimensions the array is going to have, then for loops might be a bit tricky. Better instead to use recursion, which will more easily handle an arbitrary number of dimensions, although it might be conceptually harder to implement.
                      • 8. Re: retrieving all possible terms from multidimensional array
                        800323
                        The easiest way would be to nest 3 for loops.
                        From the OP's examples, it appears that both dimension sizes are unknow; therefore, I think using recursion might be the most straight forward way.

                        <some time later>
                        Wow, you read my mind!

                        Message was edited by:
                        jbish
                        • 9. Re: retrieving all possible terms from multidimensional array
                          807607
                          what if there's a multidimensional array that consist of say 10 arrays of varied sizes?

                          stringArray[0] = [0][1]
                          stringArray[1] = [0]
                          stringArray[2] = [0][1][2][3]
                          stringArray[3] = [0][1][2]
                          stringArray[4] = [0][1][2][3][4]
                          stringArray[5] = [0][1]
                          stringArray[6] = [0]
                          stringArray[7] = [0][1][2][3]
                          stringArray[8] = [0]
                          stringArray[9] = [0][1]

                          this would be an extreme example... but could happen

                          ----

                          thought i had posted this before i left for lunch... but yeah. dimensions are unknown.

                          Message was edited by:
                          blazinbobo
                          • 10. Re: retrieving all possible terms from multidimensional array
                            807607
                            what if there's a multidimensional array that consist
                            of say 10 arrays of varied sizes?
                            Does't reply #2 answer that?

                            e.g.,
                            [0][0][0][0][0][0][0][0][0][0]
                            [0][0][0][0][0][0][0][0][0][1]
                            [0][0][0][0][0][0][0][1][0][0]
                            [0][0][0][0][0][0][0][1][0][1]
                            [0][0][0][0][0][0][0][2][0][0]
                            [0][0][0][0][0][0][0][2][0][1]
                            [0][0][0][0][0][0][0][3][0][0]
                            [0][0][0][0][0][0][0][3][0][1]

                            ... and so on.

                            [EDIT] Never mind, didn't know that was the OP.

                            ~
                            • 11. Re: retrieving all possible terms from multidimensional array
                              807607
                              what if there's a multidimensional array that consist
                              of say 10 arrays of varied sizes?
                              Doesn't matter. Just check the length of each array as you do something with it. A recursive method that does that might look generally like this:
                              public void recursiveMethod(String[]... array)
                              {
                                   for(int i = 0; i < array.length; i++)
                                   {
                                        recursiveMethod(array);
                                   }
                              }

                              EDIT: Oops, varargs won't accept multi-dimensional arrays like that... how do you do it?
                              • 12. Re: retrieving all possible terms from multidimensional array
                                807607
                                > How do you do it?

                                A quick/dirty solution might be to just accumulate the strings...
                                class Foo {
                                    
                                    public static void main(String[] args) throws Exception {
                                        String[][] array = {
                                            { "blue" },
                                            { "glove", "hat", "sock", "shoe" },
                                            { "soft", "hard" },
                                            { "short" }
                                        };
                                        
                                        doThatVoodooToThe(array, "", 0);
                                    }
                                    
                                    static void doThatVoodooToThe(String[][] array, String prefix, int row) {
                                        if (row == array.length - 1) {
                                            for (String s : array[row]) 
                                            System.out.println(prefix + s);
                                        } else {
                                            for (String s : array[row]) {
                                                doThatVoodooToThe(array, prefix + s + " ", row + 1);
                                            }
                                        }
                                    }    
                                }
                                Output:
                                blue glove soft short
                                blue glove hard short
                                blue hat soft short
                                blue hat hard short
                                blue sock soft short
                                blue sock hard short
                                blue shoe soft short
                                blue shoe hard short
                                I'm sure there are more elegant solutions.

                                ~
                                • 13. Re: retrieving all possible terms from multidimensional array
                                  807607
                                  A quick/dirty solution might be to just accumulate
                                  the strings...
                                  No, I mean how do you pass an array of unknown dimensions as an argument? Your example will still only work if you know the number of dimensions beforehand.

                                  EDIT: Don't mind me - I don't know what I'm talking about. It's all two-dimensional. I still want to know how to pass n-dimensional arrays though.

                                  Message was edited by:
                                  Mr_Evil
                                  • 14. Re: retrieving all possible terms from multidimensional array
                                    800323
                                    EDIT: Don't mind me - I don't know what I'm talking
                                    about. It's all two-dimensional. I still want to know
                                    how to pass n-dimensional arrays though.
                                    As an Object - not particularly useful, though.
                                    1 2 Previous Next