public class Stack <E> {
public Stack()
public void push (E e)
public E pop ()
public boolean isEmpty()
public void pushAll(Iterable<E> src)
public void popAll(Collection<E> dst)
public static void main(String[] args) {
// Exercise for pushAll, popAll
Stack <Number> s1 = new Stack<Number>
Integer i = 1; s1.push(i);
i = 2; s1.push(i);
Collection<Integer> integers = new HashSet<Integer> ();
integers.add(2);
integers.add(3);
s1.pushAll(integers); // doesn't compile
Collection<Object> result = new ArrayList<Object> ();
s1.popAll(result); // doesn't compile
for (Object n : result)
System.out.println(n);
}
}
Explain the client code, why it's desirable,
and why the pushAll()
and popAll()
methods, as written, are a problem.
Fix the problematic methods.
Explain why popAll()
has a void return type
(unlike its companion method pop()
)