Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy stack to array

Tags:

java

This is a version of the algorithm I have:

public void copyStackToArray(Stack<Integer> stack) {
    int i = 0;
    while (!this.bestRouteStack.empty()) {
        this.array[i++] = stack.pop();
    }
}

(the bounds on the array are guaranteed to be okay here for my code)

I'm wondering if there is a library algorithm that does this, but a search turned up nothing.

like image 470
user473973 Avatar asked Jan 06 '14 21:01

user473973


1 Answers

Stack subclasses Vector which already supports this, try this...

stack.toArray(array)

Here is the Javadoc for this.

like image 125
Todd Avatar answered Sep 22 '22 02:09

Todd