Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: [HELP] Iterating through a jagged edged array

1. ## [HELP] Iterating through a jagged edged array

Hi everyone,

Just found this forum (thanks google) and was pleased to see there was a section dedicated to collections and generics..

I need some help with an iteration. I have an array of arrays, something like this:

`String [][]variations = new String[6][];`

Now for each of the arrays stored in this array, the length isn't known until runtime (because the values are stored from values in a database)..

At the moment, I'm testing the iterations on a set that looks something like this:

```variations[0].length = 1;
variations[1].length = 1;
variations[2].length = 1;
variations[3].length = 4;
variations[4].length = 2;
variations[5].length = 1;```

What I am trying to achieve, is to iterate through this array of arrays, and produce a string of all possible combinations. Using the set above, there are 8 different possible combinations, something like this:

```[1a][2a][3a][4a][5a][6a]
[1a][2a][3a][4a][5b][6a]
[1a][2a][3a][4b][5a][6a]
[1a][2a][3a][4b][5b][6a]
[1a][2a][3a][4c][5a][6a]
[1a][2a][3a][4c][5b][6a]
[1a][2a][3a][4d][5a][6a]
[1a][2a][3a][4d][5b][6a]```

I hope this is making sense..

Anyway, does anyone know an easy way to do this? This is the code I'm using at the moment.. It works prefectly but it's pretty messy..

```for (int la = 0; la < variations[0].length; la++) {
for (int lb = 0; lb < variations[1].length; lb++) {
for (int lc = 0; lc < variations[2].length; lc++) {
for (int ld = 0; ld < variations[3].length; ld++) {
for (int le = 0; le < variations[4].length; le++) {
for (int lg = 0; lg < variations[5].length; lg++) {
StringBuffer sb = new StringBuffer();
sb.append(variations[0][la]);
sb.append(";");
sb.append(variations[1][lb]);
sb.append(";");
sb.append(variations[2][lc]);
sb.append(";");
sb.append(variations[3][ld]);
sb.append(";");
sb.append(variations[4][le]);
sb.append(";");
sb.append(variations[5][lg]);
System.out.println(sb);
}
}
}
}
}
}
}```

Thanks.

2. ## Re: [HELP] Iterating through a jagged edged array

A 2-D array in java is just an array of arrays. So, if you get each single array you can reduce the 2D problem down to a 1D problem:

```int[][] example = new int[5][];
// create a jagged array
int[0] = {1,2,3,4,5};
int[1] = {6,7,8};
int[2] = {9,10,11,12};
int[3] = {13,14,15,16,17,18,19,20};
int[4] = {21};

for (int i = 0; i < example.length; i++)
{
for (int j = 0; j < example[i].length; j++)
{
System.out.println("At example[" + i + "]["+j+"]: " + example[i][j]);
}
}```

3. ## Re: [HELP] Iterating through a jagged edged array

Hi, thanks for the reply, however this is not the issue.

I have googled a lot for the solution to my problem, and I see the same method suggested everywhere.

The problem is that I want to go through each row, and create a string with 1 string from each row, but there should be x different strings produced depending on how many strings are in each array.

Like so:

```Array1String1 + Array2String1 + Array3String1...

Array1String2 + Array2String1 + Array3String1...

Array1String1 + Array2String2 + Array3String1...```

And so on.. for now I've found a good enough solution but if you can produce a simple and quick alternative I would be very grateful.

Cheers.