Java, recursively reverse an array

2020-03-01 08:34发布

I haven't found anything with the specific needs of my function to do this, yes, it is for homework.

So I have:

public void reverseArray(int[] x) {

}

Precondition: x.length > 0

The fact that I can't have the function return anything, and the only argument is an array is leaving me stumped.

I've tried using loops along with the recursion, but everything I've tried seems to end up with infinite instances of the function being made.

I've gotten an idea/suggestion to use another function along with this one, but, how to use the original recursively is beyond me at the moment.

Any help is appreciated.

13条回答
在下西门庆
2楼-- · 2020-03-01 08:56

Since there's been no statement that loops cannot be used:

void reverseArray(int[] x) {
    if (x != null) {
        for (int i = 0; i < length.x / 2; i++) {
            int j = (length.x - 1) - i;
            int temp = x[i];
            x[i] = x[j];
            x[j] = temp;
         }
         reverseArray(null);
     }
}

Probably the fastest of the lot.

查看更多
Bombasti
3楼-- · 2020-03-01 08:57

Calling reverseArray(0, n, arr) here n is length of array

public void reverseArray(int i, int n, int [] arr)
{
   if(i==n)
   {
     return ;
   } 
   else
   {
     reverseArray(i+1, n, arr);
     System.out.println(arr.at(i));
   }
}
查看更多
对你真心纯属浪费
4楼-- · 2020-03-01 08:59
private static void reversePrint(int[] numbers)
{
    if(numbers.length==0) {
        return;
    }
    int[] a = new int[numbers.length -1];
    for(int i =0;i<numbers.length-1;i++) {
        a[i] = numbers[i+1];
    }
    reversePrint(a);
    System.out.println(numbers[0]+" ");

}
查看更多
老娘就宠你
5楼-- · 2020-03-01 09:00
public class FunWithAlgorthims {


public static void main(final String[] args) {

    String[] array = {"a", "b", "c", "d"};
    printArray(array);
    revereArrayRecusrive(array, 0);
    printArray(array);
}


public static void revereArrayRecusrive(final String[] array, int startPointer) {
    if (startPointer >= (array.length / 2)) {
        return;
    }
    String temp = array[startPointer];
    array[startPointer] = array[array.length - 1 - startPointer];
    array[array.length - 1 - startPointer] = temp;
    revereArrayRecusrive(array, ++startPointer);
}

public static void printArray(final String[] array) {
    Arrays.stream(array).forEach(a -> System.out.print(a + " "));
    System.out.println();
}

}

查看更多
手持菜刀,她持情操
6楼-- · 2020-03-01 09:03

psuedo code

function revarray(a[1...n])
  if a.length == 1 or a.length == 0
    do nothing 
  # return a
  else
     swap a[1] and a[n]
     revarray(a[2...n-1])
  # return a (The function will not return anything but the contents of a are changed)
查看更多
聊天终结者
7楼-- · 2020-03-01 09:06

This is probably the easiest way, not the fastest, but probably the easiest.

A whole program would look something like this:

public static void main(String [] args)
{
    BackwardsArray back = new BackwardsArray();
}

public BackwardsArray()
{
    int [] a = {1,2,3,4,5,6,7,8,9};
    printBackwards(a);
}

void printBackwards( int [] b)
{
    print(b,b.length-1);
}

void print(int [] b, int pos)
{
    System.out.println(b[pos]); // prints last item
    if(pos != 0)
    {
        print(b,pos-1);
    }
}

Hope it helps!

查看更多
登录 后发表回答