# Multiple for loops ?

Q : Is there any way to avoid using multiple for loop ?

For example :
I have three numbers say 1, 2 and 3
1 represents A, B, C, D, E
2 represents F, G, H, I,
3 represents K, L, M, N,

I want to produce an output combination of these options like
AFK
AFL
AFM
...
...
...
so on..

In this example I have 3 numbers but practically I have 10 Numbers.

What I have done is looped in 10 for loops and getting the result inside inner most loop.
Working fine ! but it looks ugly

Again I have a problem, If user asks for comination of 1,2,3 and 6,7,8,9 ( skipped 4, 5)
How should I proceed ???

Pl let me know if U R not clear with the question.

Thanks,
Kishor

###### Who is Participating?

Commented:
Say,
1 represents A, B, C, D, E (call it section 1)
2 represents F, G, H, I, (call it section 2)
3 represents K, L, M, N, (call it section 3 etc.)

and we have to create create all possible 3 char words
so that i'th char is from i'th section right?
Here we go:
import java.util.*;
class ManyLoops
{
Vector sectors;
Vector words;
public ManyLoops(){
sectors = new Vector();
words = new Vector();
}
public static void main(String[] args){
ManyLoops manyLoops = new ManyLoops();
manyLoops.init();
manyLoops.makeWords();
manyLoops.publish();
}

public void init(){
}

public void makeWords(){
for(int j=0; j<sectors.size();j++){
extend((String)sectors.elementAt(j));
}
}

public void extend(String sector){
int len = words.size();
for(int i=0; i<len;i++){
for(int j=0; j<sector.length()-1;j++){
}
}

for(int i=0; i<len;i++){
words.setElementAt(
(String)words.elementAt(i)+
sector.substring(sector.length()-1,sector.length()), i
);
}

}

public void publish(){
//prints out all words it knows:
for(int i=0; i<words.size();i++){
System.out.println((String)words.elementAt(i));
}
}
}

0

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

--  points to bagi

sudhakar_koundinya
EE Cleanup Volunteer
---------------------