- #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?
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);
}
}