Java 如何使数组按相反顺序返回?

Java 如何使数组按相反顺序返回?,java,arrays,reverse,Java,Arrays,Reverse,我需要做的是在方法之前的注释中。A保存一个10元素长的数组,用户输入: //Reverses the contents of the array. The array remains unchanged //It prints the reversed array to the screen static void reverseArray(int [] A) { int[] tempArray = new int[10]; for(int

我需要做的是在方法之前的注释中。A保存一个10元素长的数组,用户输入:

//Reverses the contents of the array. The array remains unchanged
    //It prints the reversed array to the screen
    static void reverseArray(int [] A)
    {
        int[] tempArray = new int[10];

      for(int i = A.length-1; i >= 0; i--)
      {
        //Supposed to copy what's in reversed array A to tempArray
        tempArray[i] = A[i];
      }
        printArray(tempArray); //Prints the updated array to the screen
    }

我想让它做的是从A的最后一个元素倒数到第一个元素,然后将其复制到tempArray。但现在它只打印用户输入的数组。我知道我需要2个整数来跟踪递增和递减的内容,但我不知道如何实现它们。

首先,不要硬编码
tempArray
的长度。使用
A的长度

int[] tempArray = new int[A.length];
其次,将
A
的每个元素复制到
tempArray
的反向索引中:

for(int i = A.length-1; i >= 0; i--) {
  tempArray[A.length-1-i] = A[i];
}
这是我的方法

    static void reverseArray(int [] A){
        int[] tempArray = new int[A.length];
        for(int i = 0; i < A.length; i++){
             tempArray[i] = A[A.length - i - 1];
        }
        printArray(tempArray); //Prints the updated array to the screen
    }

    static void printArray(int[] array){
        for(int i = 0; i < array.length; i++){
            System.out.print(array[i] + " ");
        }
    }
static void reversearlay(int[]A){
int[]tempArray=newint[A.length];
for(int i=0;i
所以我看到了,这真的很有帮助。我也可以按照我最初的计划使用for循环,但是while循环也可以很好地工作。前者可能更容易,因为我不需要做更多的变量,但这没关系

static void reverseArray(int [] A)
    {
      int i = A.length - 1;
      int j = 0;
      int[] tempArray = new int[A.length];

      while(i >= 0)
      {
        tempArray[j] = A[i];
        i--;
        j++;
      }
        printArray(tempArray); //Prints the updated array to the screen
    }

想想看。输入数组的索引
0
处的项必须位于输出数组的最后一个索引处(此处的索引
9
)。然后继续执行以下索引。ArrayUtils.reverse(int[]数组)首先,查找适当的重复项?