题解 | #数组倒转#
数组倒转
https://www.nowcoder.com/practice/3a90caee736a45e1ad70b1d4105dec5f
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { int[] arr = new int[6]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < arr.length; i++) { arr[i] = scanner.nextInt(); } System.out.println(Arrays.toString(arr)); //write your code here...... //把数组先复制到一个新的数组newArr int[] newArr=new int[6]; for (int i = 0; i < arr.length; i++) { newArr[i]=arr[i]; } //然后将数组newArr的值倒着遍历赋给arr就ok了 int i=0; for(int j=arr.length-1;j>=0;j--){ arr[i]=newArr[j]; i++; } System.out.println(Arrays.toString(arr)); } }