【发布时间】:2016-07-24 14:47:26
【问题描述】:
所以我很难想出一个函数,它以 (int[] X, int n, int[] Y) 作为参数并将 X[] 中最左边的 n 个元素复制到最右边的 n Y[] 中的位置以相反的顺序排列。
到目前为止,我已经创建了一个单独的函数,可以打印出 A 中最左边的 n 元素的反转。
public static void reverseArray1(int[] A, int n) {
if(n > 0) {
System.out.print(A[n-1] + " ");
reverseArray1(A, n-1);
}
}
这是我现在的程序:
class Recursion {
static void reverseArray1(int[] X, int n, int[] Y) {
//This is where I'm stuck
}
public static void main(String[] args) {
int[] A = {-1, 2, 3, 12, 9, 2, -5, -2, 8, 5, 7};
int[] B = new int[A.length];
for(int x: A) System.out.print(x+" ");
System.out.println();
reverseArray1(A, A.length, B);
for(int x: B) System.out.print(x+" ");
System.out.println();
}
}
【问题讨论】:
-
欢迎来到 SO。你能再补充一点吗,比如写一个测试方法来调用 reverseArray1 用一个 [1, 2, 3, 4] 的 int 数组和一个 3 的 n?也许打印结果并说出您的期望?
标签: java arrays recursion data-structures