Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Lists and Sub-arrays

Options
  • 23-06-2009 11:34am
    #1
    Registered Users Posts: 14,317 ✭✭✭✭


    Consider the following code and test data...
    String[] strArray1 = {"1a", "1b", "1c"};
    String[] strArray2 = {"2a", "2b"};
    String[] strArray3 = {"3a", "3b", "3c", "3d"};

    List<String[]> listStrArray = new ArrayList();
    listStrArray.add(strArray1);
    listStrArray.add(strArray2);
    listStrArray.add(strArray3);

    Now consider that the length of each array is totally variable.
    Also, the actual number of arrays in listStrArray is variable.
    The sample I have provided is one typical set of data, there could be any number of arrays in in listStrArray and there could be any amount of elements in each element in each array.

    I want to iterate over each element in listStrArray and each element in the array within each. So if I was to print each element out as I dealt with it, this is the order I want to see them in...
    1a, 2a, 3a
    1a, 2a, 3b
    1a, 2a, 3c
    1a, 2a, 3d

    1a, 2b, 3a
    1a, 2b, 3b
    1a, 2b, 3c
    1a, 2b, 3d

    1b, 2a, 3a
    1b, 2a, 3b
    1b, 2a, 3c
    1b, 2a, 3d

    1b, 2b, 3a
    1b, 2b, 3b
    1b, 2b, 3c
    1b, 2b, 3d

    and so on.

    I hope I have clearly illustrated what I am trying to do.
    I cannot for the life of me figure out how to do this, forest+trees and all that.

    If any one can provide an insight, I would really appreciate it.


Comments

  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    You basically want your loops to reset when they have reached the end for each array, and terminate completely when all arrays have reached their last value. You can use a recursive looping algorithm

    I'll post more if this doesn't help / you've figured that much ;)


  • Registered Users Posts: 14,317 ✭✭✭✭Raam


    thanks for the response.

    I'm struggling with the number of loops I need and how to deal with the situation where I have more than 2 string arrays, if that makes sense.

    I cannot see how I can pick the first element in strArray1, then iterate over all in strArray2 and all in strArray3, such that I follow the pattern in my first post

    Forgive me if I ramble a bit, I'm still trying to tease it out in my own head :o


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Visual it without trying to be dynamic first.
    A three tier nested loop is how you would print out that pattern if you were to do it statically.
    for(int i= 0;i < strArray1.Length; i++)
                {
                    for (int j = 0; j < strArray2.Length; j++)
                    {
                        for (int k = 0; k < strArray3.Length; k++)
                        {
                            somestring += strArray1[i] + " " + strArray2[j] + " " + strArray3[k] + "\n";
                        }
                    }                     	
                }
    

    Seeing as you need to dynamically add loops when you have more arrays, you can use a recursive function to replace manually creating the loops. You'll need a modifier PER loop, and the final loop needs to output the sequence. So a loop function that takes in how many loops to run on an array that's also passed in.

    Think about what you need to pass into a function, how to make the function call itself with different data, and how to ensure you can traverse the entire list, maintaining the indexes of each array you need to print out. Obviously you do not need to recursively call the function on the last array entered into the list.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Balls, didn't read your post properly at all :P


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    I think this is pretty simple Raam, you just want to re-org the data into columns.

    Make this easy for yourself. You want to make all the permutations.

    Think of each block of output you showed us above as a list of strings.
    List<String> = new ArrayList<String>();
    

    This depends on each 2a, 2b, etc. Forget about the 1a, 1b for the moment, just pass it into as a method parameter that is used to start each line that prints out 2a, 3x, 2a, 3y...

    Now then call that method from something that iterates over 1x, 1y.

    Does that make sense?


  • Advertisement
  • Registered Users Posts: 14,317 ✭✭✭✭Raam


    Thanks for all the help guys.
    I think I nailed it with a recursive function. Not a huge fan of them as they are tricky to debug, but it looks like it's done the job for me.


  • Registered Users Posts: 40 dob99


    You shouldn't need to resort to recursion with the data structures you've defined. Hitcher wasn't far off.

    First of all, declare your List as follows (just for consistency really):
    List<String[]> listStrArray = new ArrayList<String[]>();
    

    Then you'll use an iterator for the outer array and just normal array access for the inner ones:
    for (Iterator<String[]> it=listStrArray.iterator(); it.hasNext(); ) {
      String inner[] = it.next();
      for (int i=0; i<inner.length; i++) {
        System.out.print(inner[i]);
        System.out.print(", ");
      }
      System.out.println();
    }
    

    I haven't tried the code, so there might be minor errors, but it should be enough to get you started.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Thats similar to what I previously posted dob but I removed it, look at the way the data is being printed out.


Advertisement