I am attempting to reverse an array using a stack. However, I get an error on arr[i] = stack.top();, and the suggestion to resolve it in Eclipse is to change it to arr[i] = stack.pop(); or to add a cast. Is there another way about this or have I made a mistake?
I see tutorials and questions asking about how to reverse a string using a stack and I have tried to reverse an array using the same logic, but I'm not entirely sure where I'm going wrong.
public static void reverse(String[] arr){
Stack stack = new Stack();
for(int i = 0; i < arr.length; i++) {
stack.push(arr[i]);
}
for(int i = 0; i < arr.length; i++) {
arr[i] = stack.top();
stack.pop();
}
return;
}