KaliMa KaliMa - 1 year ago 94
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?

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

Those operations are constant time.