执行以下操作:
public class Test {
public static void main(String[] args) {
Test x = new Test();
x.generatePermutations(2, 3, 1);
}
private void generatePermutations(int intervalOne, int intervalTwo,
int intervalThree) {
for (int i = 0; i <= intervalOne; i++) {
for (int j = 0; j <= intervalTwo; j++) {
for (int k = 0; k <= intervalThree; k++) {
System.out.print(i + "" + j + "" + k + " ");
}
}
}
}
}
编辑:我认为这会做一个任意数量的间隔数组:(你必须检查它的正确性:
public class Test {
public static void main(String[] args) {
Test x = new Test();
int[] intervals = { 1, 1, 1, 1, 1 };
x.generatePermutations(intervals);
}
private void generatePermutations(int[] intervals) {
generatePermutations(intervals, 0, "");
}
private void generatePermutations(int[] intervals, int intpos,
String lastPerm) {
if (intpos == intervals.length)
return;
for (int i = 0; i <= intervals[intpos]; i++) {
if (intpos == intervals.length - 1) {
System.out.print(lastPerm + i + " ");
}
generatePermutations(intervals, intpos + 1, lastPerm + i);
}
}
}