C
Chris
Help please. I want to print out all the ordered permutation that a
list of items can take but the problem here is I need as many forloops
as there are items and in the real program it is not possible to
determine that at compile time. To solve this I would obviusly need
to do it recursively but I simply can't think in terms of recursion.
Any kind soul wanna help out PLEASE?
public static void main(String[] args){
int numberofitems =4;
int maxnumberofvaluesforanitem = 3;
Vector firstitem = new Vector();
firstitem.addElement("1");
firstitem.addElement("2");
firstitem.addElement("3");
Vector seconditem = new Vector();
seconditem.addElement("4");
seconditem.addElement("5");
Vector thirditem = new Vector();
thirditem.addElement("6");
thirditem.addElement("7");
Vector fourthitem = new Vector();
fourthitem.addElement("8");
fourthitem.addElement("9");
for(int a=0; a<firstitem.size();a++){
for(int b=0;b<seconditem.size();b++){
for(int c=0;c<thirditem.size();c++){
for(int d=0;d<fourthitem.size();d++){
System.out.println((String)firstitem.elementAt(a) +
(String)seconditem.elementAt(b) +
(String)thirditem.elementAt(c) +
(String)fourthitem.elementAt(d) );
}
}
}
}
Would result
1,4,6
1,4,7
2,4,6
2,4,7
3,4,6
3,4,7
1,5,6
1,5,7
2,5,6
2,5,7
3,5,6
3,5,7
list of items can take but the problem here is I need as many forloops
as there are items and in the real program it is not possible to
determine that at compile time. To solve this I would obviusly need
to do it recursively but I simply can't think in terms of recursion.
Any kind soul wanna help out PLEASE?
public static void main(String[] args){
int numberofitems =4;
int maxnumberofvaluesforanitem = 3;
Vector firstitem = new Vector();
firstitem.addElement("1");
firstitem.addElement("2");
firstitem.addElement("3");
Vector seconditem = new Vector();
seconditem.addElement("4");
seconditem.addElement("5");
Vector thirditem = new Vector();
thirditem.addElement("6");
thirditem.addElement("7");
Vector fourthitem = new Vector();
fourthitem.addElement("8");
fourthitem.addElement("9");
for(int a=0; a<firstitem.size();a++){
for(int b=0;b<seconditem.size();b++){
for(int c=0;c<thirditem.size();c++){
for(int d=0;d<fourthitem.size();d++){
System.out.println((String)firstitem.elementAt(a) +
(String)seconditem.elementAt(b) +
(String)thirditem.elementAt(c) +
(String)fourthitem.elementAt(d) );
}
}
}
}
Would result
1,4,6
1,4,7
2,4,6
2,4,7
3,4,6
3,4,7
1,5,6
1,5,7
2,5,6
2,5,7
3,5,6
3,5,7