gpt4 book ai didi

java - 反转数组

转载 作者:行者123 更新时间:2023-12-02 04:07:06 24 4
gpt4 key购买 nike

我刚刚开始使用数组,想知道我必须在这段代码中添加什么才能让数组以相反的顺序打印出来。

import java.util.Scanner;
public class Program10 {

static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {

int[] myArr = {5, 3, 2, 6, 14, 8, 9, 14};
int[] myArr2 = {6, 7, 8, 9, 10};

System.out.println("First array before being arranged");
printArray(myArr);
rearrangeArray(myArr);
System.out.println("First array after being arranged");
printArray(myArr);

System.out.println("Second array before being arranged");
printArray(myArr2);
rearrangeArray(myArr2);
System.out.println("Second array after being arranged");
printArray(myArr2);

}

public static void printArray(int[] a){

}

public static void rearrangeArray(int[] b){

}
}

最佳答案

public void printRev(int[] x){
for(int i = x.length-1; i>=0; i--){
System.out.print(x[i] + " ");
}
}

在此示例中,您所要做的就是从后到前。

关于java - 反转数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com