在不使用循环的情况下,要想实现循环的效果,通常可以用递归的方式
public class Test {
static int[] a = {1, 2, 3, 4, 5, 6};
public static void main(String[] args) {
printArray(a, a.length);
}
public static void printArray(int[] a, int count) {
if (count > 0) {
System.out.print(a[count - 1] + "\n");
printArray(a, count - 1);
}
}
}
输出结果: