Understanding Java Recursive Array Reversal

In summary, The code snippet provided is attempting to reverse an array of strings recursively. The array is passed in as an object and the rev() function is called, which then calls the private rev() function. Within the rev() function, the first and last elements of the array are swapped, and then the rev() function is called again with the first and last indices incremented and decremented, respectively. This process continues until the last index reaches 0, at which point the array should be fully reversed. However, the output shows that the array has been reversed in a seemingly random order, which the person is unsure of why. They suggest hand-simulating the process to better understand what is happening.
  • #1
apiwowar
96
0
supposed to reverse an array of strings recursively. this is in a class that has an array object.

i came up with what's below. but it reverses the array in a weird order. the array is a 5 element array, the elements are {hello, how, are you, ?}. i passed in the object and after printed out the array and it came out as are hello you how ? which makes no sense to me.

im not sure why it printed out like that, shouldn't it just run through swapping first and last?

Code:
// reverse an array 
public void rev() 
{
    rev(0,a.length-1);
} 

private void rev(int first, int last)
{
    if(last == 0)
    {
    //do nothing
    }

    else
    {


    String temp = a[first];
    a[first] = a[last];
    a[last] = temp;

    
    rev(first+ 1, last-1);

    }
}
 
Physics news on Phys.org
  • #2
Take a piece of paper and a pencil, and hand-simulate what is happening to your array (which I assume is an array of character strings).

The first call to rev comes in your main function. What happens? What happens in the recursive call to rev. Follow all the way through until last is 0.
 

Related to Understanding Java Recursive Array Reversal

1. What is a recursive function in Java?

A recursive function in Java is a function that calls itself repeatedly until a base condition is met. This allows for repetitive tasks to be performed more efficiently and elegantly.

2. How does a recursive function reverse an array in Java?

A recursive function reverses an array in Java by breaking down the task into smaller subtasks. It first swaps the first and last elements of the array, then recursively calls itself on the remaining elements until the entire array is reversed.

3. What is the base case in a recursive function for reversing an array in Java?

The base case in a recursive function for reversing an array in Java is when the function reaches the middle of the array. At this point, all elements have been swapped and the function can stop calling itself and return the reversed array.

4. What are the advantages of using recursion to reverse an array in Java?

One advantage of using recursion to reverse an array in Java is that it can be a more elegant and concise solution compared to iterative methods. It also allows for efficient use of memory and can handle arrays of any size without causing stack overflow.

5. Are there any potential drawbacks to using recursion to reverse an array in Java?

One potential drawback of using recursion to reverse an array in Java is that it may not be as efficient as an iterative solution, especially for larger arrays. It also requires a good understanding of recursion and can be more difficult to debug and troubleshoot compared to iterative methods.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
996
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top