KaliMa KaliMa - 4 months ago 65
Scala Question

Efficient retrieval of last and second to last element of ArrayStack in Scala?

I am using a mutable

in Scala but do not know how to access the last element (and second to last element) efficiently (constant time) without popping the items from the stack. Is it possible to access the elements?

stack(4)    // returns 5th element
stack.last  // returns last element

Those operations are constant time.