, :
static final int UP = -2, DOWN = -1;
static void loop1(int[][][] A, int t1, int t2, int t3) {
switch (t1) {
case UP:
for (int i = 0; i < A.length; i++)
loop2(A[i], t2, t3);
break;
case DOWN:
for (int i = A.length - 1; i >= 0; i--)
loop2(A[i], t2, t3);
break;
default:
loop2(A[t1], t2, t3);
}
}
static void loop2(int[][] A, int t2, int t3) {
switch (t2) {
case UP:
for (int i = 0; i < A.length; i++)
loop3(A[i], t3);
break;
case DOWN:
for (int i = A.length - 1; i >= 0; i--)
loop3(A[i], t3);
break;
default:
loop3(A[t2], t3);
}
}
static void loop3(int[] A, int t3) {
switch (t3) {
case UP:
for (int i = 0; i < A.length; i++) {
System.out.println(A[i]);
}
break;
case DOWN:
for (int i = A.length - 1; i >= 0; i--) {
System.out.println(A[i]);
}
break;
default:
System.out.println(A[t3]);
}
}
FIXEDis the only option that requires an index, and therefore is encoded as an index. UPand DOWNare not indices, therefore they are encoded using negative numbers. Usage will be similar to
public static void main(String[] args) {
int[][][] m = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
loop1(m, DOWN, 1, UP);
}
With my example, the System.out.printlnoutput will be
7
8
3
4
source
share