1. Create a StackIterator class that can be used to iterate thru the contents of
ID: 3530689 • Letter: 1
Question
1. Create a StackIterator class that can be used to iterate thru the contents of a Stack. Using the ListIterator class as a guide, this StackIterator class should support the following public interface: bool StackIterator::isValid() const; void StackIterator::advance(); const Object& StackIterator::retrieve() const; Modify the Stack class so instances have the ability to create iterators by adding the method: template StackIteratorStack::first(); Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Stack before and after invoking the operation. 2. Create a new method on a Stack that determines if a passed value is not found on the Stack. For example, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), notFound( 3 ) should return false because there are values on the stack that equal 3. On the other hand, for a stack containing top- (1) (1) (2) (3) (5) (5) (2), notFound( 12 ) should return true because there are no values on the stack that equal 12. This new operation should have the signature: bool Stack::notFound( const Object & data ) const; Create a test driver program that exercises each of these exercises to satisfy yourself that your implementation is correct. For example, you should print the Stack before and after invoking the operation.Explanation / Answer
If you're only interested in the keys, you can iterate through the keySet() of the map: Map map = ...; for (String key : map.keySet()) { // ... } If you only need the values, use values(): for (Object value : map.values()) { // ... } Finally, if you want both the key and value, use entrySet(): for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ... } One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator (see karim79's answer). However, changing item values is OK (see Map.Entry).
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.