Java, recursively reverse an array

reverse list java recursion
reverse array recursion javascript
reverse array java
recursive method to reverse array java
reverse string array java
reverse order java
reverse first and last element in array
how to reverse an array in c++

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.

If I were coding this, I would create a temporary array (maybe with one element removed?) for the recursive call and copy elements back into the original array before returning from the function. You will also need to find a base case to terminate the recursion.

Reverse an Array using Recursion - Java Code, Reversing an array using Recursion is an example of Tail Recursion . We maintain two in-variants “i” and “j”. “i” holds starting element index  Reversing an array in java can be done in three simple methods. Examples: Input : 1, 2, 3, 4, 5 Output :5, 4, 3, 2, 1 Input : 10, 20, 30, 40 Output : 40, 30, 20, 10. The first method is as follows: (i) Take input the size of array and the elements of array. (ii) Consider a function reverse which takes the parameters-the array (say arr) and the size of the array (say n).

void reverseArray(int[] x){
   reverse(x, 0, x.length -1);

void reverse(int[] x, int i, int j){
       int tmp = x[i];
       x[i] = x[j];
       x[j] = tmp;
       reverse(x, ++i, --j);//Recursive


int[] s = new int[]{1,2,3,4,5};

Recursive, O(n), no temporary Array needed.

Reversing an array using Recursion in Java– J2eeDev, In this post, we will see how to reverse an array in Java. We can also use recursion for reversing an array without using an auxiliary array. The logic remains  Reversing an array using Recursion is an example of Tail Recursion . We maintain two in-variants “i” and “j”. “i” holds starting element index and “j” holds ending element index of the array. As long as “i” is less than “j”, we swap two elements starting and ending element of the array.

Because this is your homework, I suggest an example :

Given sequence : 1 2 3 4 5 6 7 8 9 10

You can change to : 10 2 3 4 5 6 7 8 9 1

After that: 10 9 3 4 5 6 7 8 2 1


As you see, step by step, the sequence is "better" and the problem is "smaller". So, the problem you should solve to complete is :

1) How to apply recursive call for this method. for the original, the method is : reverse(int[] a). so, after first step, you should create array b from a[2] --> a[n-1]. and using reverse(int[] b)`.

2) after reverse b, what should we do to reverse a ? Assign values of b again back to a.

3) stop condition : what stop condition ? You see that elements of array b less than elements of array a. So, to which step, we should stop ?

Hope this help :)

Write a program to reverse an array or string, Reverse input array using iterative & recursive algorithm. Example to reverse an integer array using java. Let input array is { 1, 2, 3, 4, 5 }; Reverse  That's all about how to reverse a list in Java. You have seen 2 ways to reverse the list, first by using the Collections.reverse() method, which you should use to sort any List implementation e.g. Vector, LinkedList, CopyOnWriteArrayLis, or ArrayList in Java in production. The recursive algorithm is just for educational purpose.

public class RecursiveArray {

   public static int[] backWardArray(int[] arr, int start, int end) {

       if (start < end) {
           int temp = arr[start];
           arr[start] = arr[end];
           arr[end] = temp;
           backWardArray(arr, start + 1, end - 1);
       return arr;

    public static void main(String[] args) {
        int [] arr = {12,4,6,8,9,2,1,0};
    int [] reversedArray= backWardArray(arr, 0, arr.length-1);
    //loop through the reversed array
        for (int i: reversedArray) {

    public RecursiveArray() {

Reverse an array in Java, Reverse Integer Array Recursively in Java. Recursion can be used to reverse an integer array without allocating a new array. This method is  Program to reverse a string (Iterative and Recursive) First string from the given array whose reverse is also present in the same array; Program to reverse an array using pointers; Program to reverse the rows in a 2d Array; Reverse the substrings of the given String according to the given Array of indices

Try something as below:

public void reverseArray(int[] x) {
    if(x.length ==2){
      //if two elements, swap them
      int first = x[0];
      x[0] = x[1];
      x[1] = first;
    }else if(x.length > 2){
      //swap first and last
      int first = x[0];
      x[0]= x[x.length-1];
      x[x.length-1] = first;
      //create a copy of middle elements
      int [] copy = new int[x.length-2];
      System.arraycopy( x, 1, copy, 0, x.length-2);
      //recursive call for middle elements
      //place the reversed elements back in the original array
      System.arraycopy( copy, 0, x, 1, copy.length);

Reverse an integer array in java using iterative & recursive algorithm, import java.util.ArrayList;. import java.util.Iterator;. import java.util.​List;. public class Recur {. private static List<String> list = new ArrayList<String>  One way to know when to stop making the recursive call could be to pass a smaller array to the function each time, and stop when you have an empty array. This would work better in a language like C where arrays are exposed as pointers, but in Java it would be inefficient as you would have to create a new array each time.

Reverse Integer Array Recursively in Java, To reverse an array, you need to change the elements of the array but the search Function to reverse an Array using recursion */ void reverse(int *array, write a java program to create a class named "Online Account" with email and  Print array using recursion JAVA Example in Recursion - Data structures and Algorithms by Java Examples.

Recursive function to reverse order of a String List in Java · GitHub, Java, recursively reverse an array, 1) How to apply recursive call for this method. for the original, the method is : reverse(int[] a) . so, after first step, you should  I am trying to recursively reverse the elements of an array. The only parameters I can have in the function is the array and the size. This is what I have done so far, but having trouble in swapping.

how to reverse the elements of an array using recursion?I tried it but ,

  • You can change the data inside the array on your method. There's nothing wrong reversing an array in a method that returns nothing. It would be good to post your idea and we gladly give you advices (not code) from there.
  • By the way, can you add another parameter to your reverseArray method?
  • We cannot add another parameter, if that was so, I know how I would do it. And yes, helper methods were mentioned as a viable option, I'm just not sure how that would be implemented.
  • @HotLicks, extra parameters are forbidden, I think.
  • If you can't have any extra parameters (and you can't have extra elements in the array to act like parameters) and you can't create copies of the array then you can't do it. If you can make copies (and use System.arraycopy) then you can do it, by passing shorter and shorter copies of the array.
  • +1 The temporary array that is being passed to reverseArray will be one element shorter every step (so that recursion terminates after N steps).
  • @Thilo I decided my original answer had too many details, so I rewrote it.
  • I had this as an idea, I just wasn't sure how to implement it earlier, I think I may have it figured out now. Pass in a stripped down array (first and last elements removed) into a new instance of the function, until the length is 2, switch them, (if length == 1, do nothing), and then from thereon, on each end of an instance, tack on the removed elements, but switched?
  • This put me on the right path without telling me exactly what to do, much appreciated.
  • @dabbertorres I only had in mind removing either the first or last element. I like your idea to remove both and switch them.
  • The above doesn't meet the restriction (perceived by some) that you can't have any extra arguments. But it's the most obvious solution to me (and the one I suggested above).