KaliMa KaliMa - 1 month ago 26
Scala Question

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

I am using a mutable

ArrayStack
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?

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

Those operations are constant time.