我需要编写一个方法,压缩执行以下操作;
compress 方法的目标是从堆栈 s1 中删除所有空元素。其余(非空)元素应按初始顺序保留在 s1 上。辅助堆栈 s2 应用作 s1 中元素的临时存储。在该方法的计算结束时,堆栈 s2 应该与计算开始时的内容相同。有关方法 compress 的预期行为的示例,请参见方法 main。
到目前为止,我有;
import net.datastructures.ArrayStack;
import net.datastructures.Stack;
public class Stacks {
public static <E> void compress(Stack<E> S1, Stack<E> S2) {
    int counter = 0;
    while (!S1.isEmpty()) {
    }
    if (S1.top() == null) {
        S1.pop();
    } else if (S1.top() != null) {
        S2.push(S1.pop());
        counter++;
    }
    for (int i = counter; i < counter; i++) {
        S2.push(S1.pop());
    }
}
public static void main(String[] args) {
    // test method compress
    Stack<Integer> S1 = new ArrayStack<Integer>(10);
    S1.push(2);
    S1.push(null);
    S1.push(null);
    S1.push(4);
    S1.push(6);
    S1.push(null);
    Stack<Integer> S2 = new ArrayStack<Integer>(10);
    S2.push(7);
    S2.push(9);
    System.out.println("stack S1: " + S1);
    // prints: "stack S1: [2, null, null, 4, 6, null]"
    System.out.println("stack S2: " + S2);
    // prints: "stack s2: [7, 9]"
    compress(S1, S2);
    System.out.println("stack S1: " + S1);
    // should print: "stack S1: [2, 4, 6]"
    System.out.println("stack S2: " + S2);
    // should print: "stack S2: [7, 9]"
}
}
我不知道哪里出错了,代码在 compress 方法之前打印了两行,然后什么也不打印。