0

Im trying to write a function to display all combinations in a jagged array, where each combination contains one element from each sub-array. The jagged array can consist of any number of arrays and each array can have any number of elements. E.g. for the following array: a[0] = {1, 3, 5} a[1] = {2, 4} it should return: (1, 2) (1, 4) (3, 2) (3, 4) (5, 2) (5, 4)

I thought of doing it this way but immediately run into trouble. Logically it looks OK to get 1, 2 and 1, 4 but then next run I is set back to 0 (sorry not at devel machine to test now). Can anyone suggest a better solution please?

Here is my code

for (int i = 0; i < array1.length(); i++)
    for (int j = 0; j < array2.length(); j++)

        if (j < array2.length())
            i = 0;
        else 
            i++;

        System.out.println(array1[i] "," array2[j])
3
  • what is the if else necessary for again? Commented Jun 18, 2013 at 16:53
  • I don't see the reason behind the if else you have, without it it should print all the combinations of the arrays. Commented Jun 18, 2013 at 16:54
  • if you don't want to do this then use this library Commented Jun 18, 2013 at 16:57

6 Answers 6

1

You don't need this:

if (j < array2.length())
            i = 0;
        else 
            i++;

i is incremented automatically in a for loop.

This should be fine:

for (int i = 0; i < array1.length(); i++)
    for (int j = 0; j < array2.length(); j++)
        System.out.println(array1[i] "," array2[j])
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm understanding your question correctly (which I might not be) I think all you need is just

for (int i = 0; i < array1.length(); i++){
  for (int j = 0; j < array2.length(); j++){
    System.out.println(array1[i] "," array2[j]);
  }
}

to achieve the desired result

Comments

0

How about this:

int a [] = {1,2,3}; int b[] = {1,2};

for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < a.length; j++) {
        System.out.println(a[i]+","+a[j]);

    }

}

Comments

0

Your if statement inside the loop breaks everything. You just need 2 nested loop to complete your task:

for (int i = 0; i < array1.length(); i++)
    for (int j = 0; j < array2.length(); j++) {
        System.out.println(array1[i] + "," + array2[j]);
    }
}

Comments

0
for (int i = 0; i < array1.length(); i++)
    for (int j = 0; j < array2.length(); j++)
        System.out.println("(" + array1[i] + "," array2[j] + ")");

Comments

0

Here's a general solution that works with any number of arrays (beware the exponential nature of the runtime of this algorithm):

int[][] arrays = new int[][]
{
    {1, 2, 3, 4, 5, 6},
    {1, 2, 3, 4, 5, 6}, 
    {1, 2, 3, 4, 5, 6}
}; // let's print all fair rolls of a 3d6

if (arrays.length == 0) return; // this is why we can't have nice things

int[] currentPos = new int[arrays.length];

while(currentPos[arrays.length - 1] != arrays[arrays.length - 1].length)
{
    // print the current value
    System.out.print(arrays[0][currentPos[0]]);
    for (int i = 1; i < arrays.length; ++i)
        System.out.print(", " + arrays[i][currentPos[i]]);
    System.out.println();

    // increment the "counter"
    ++currentPos[0];
    for (int i = 1; i < arrays.length; ++i)
    {
        if (currentPos[i - 1] == arrays[i - 1].length)
        {
            currentPos[i - 1] = 0;
            ++currentPos[i];
        }
        else break;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.