sách gpt4 ai đã đi

Mảng nghịch đảo (Java) // Mảng nghịch đảo (Java)

In lại 作者:行者123 更新时间:2023-12-01 18:06:37 33 4
mua khóa gpt4 Nike

我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。

public class Recursion {
public static int arrayReverse(int [] array, int indice, int pos){
if(indice>=0 && pos<>
int tmp = array[pos];
array[pos]=array[indice];
array[indice]=tmp;
arrayReverse(array, indice-1, pos+1);
}
return array[array.length-1];
}

public static void arrayReverse(int [] array){
arrayReverse(array, array.length-1, 0);
}

}主类是数组

import java.util.Arrays;

public class Main {
/**
* Prueba el método Recursion.arrayReverse
* @param input - array a tratar
* @param expected - resultado esperado
*/
static void test(int[] input, int[] expected) {
// Se informa del caso que se está probando
System.out.println("probando: arrayReverse(" + Arrays.toString(input) + ")");

// Llamada al método a probar
Recursion.arrayReverse(input);

// Comprobación de los resultados
if (!Arrays.equals(input, expected)) {
System.out.print(">> Resultado erróneo, deberia ser: " + Arrays.toString(expected) + "");
System.out.println(" y es: " + Arrays.toString(input) + "");
} khác {
System.out.println(">> Resultado correcto: " + Arrays.toString(input) + "");
}
}

/**
* Invoca a test para realizar múltiples pruebas
* @param args
*/
public static void main(String[] args) {
int[] v1 = {1, 2, 3, 4, 5};
int[] v2 = {5, 4, 3, 2, 1};
test(v1, v2);

}}

1 Câu trả lời

提示:您正在交换元素 - 这意味着您只需要迭代数组的一半...

关于java - 逆数组(Java)//逆数组(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951842/

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com