1

I am supposed to be writing a program to reverse the numbers in an array recursively. I don't really know much code at this point. This is what I have so far.

This is the reverse method itself:

  public static void reverse(int[] array, int i, int j)
  {
    if (i < j)
    {
      int temp = array[i];
      array[i] = array[j];
      array[j] = temp;
      reverse(array, ++i, --j);

I am not sure if this is even right. I then have the main method.

  public static void main(String[] args)
  {
   Scanner input = new Scanner(System.in);
   System.out.println("Enter size of the array");
   int n = input.nextInt();
   int[] array = new int[n];

   for (int i =0; i <array.length; i++)
   {
     System.out.println("Enter number to be inputed into array.");
     array[i] = input.nextInt();

   }
    System.out.println(array);
  }

It is not printing out right and prints numbers and letter in text where I think it should be the array.

4

1 回答 1

2

改成System.out.println(array);这样:

System.out.println(Arrays.toString(array));

然后您可以查看您的方法是否正确反转。目前,您没有打印数组的内容,只是打印数组所在的内存地址。

于 2013-11-05T23:16:18.677 回答