スタックs1からnull要素を取得し、それらをs2に配置するメソッドを作成するために、ここ数時間を費やしました。次に、クラスはスタックを出力する必要があります。方法は次のとおりです。
import net.datastructures.ArrayStack;
import net.datastructures.Stack;
import javax.imageio.IIOException;
public class Stacks {
public static <E> void compress(Stack<E> s1, Stack<E> s2) {
int counter = 0;
while (!s1.isEmpty()) {
s1.peek();
if (s1.peek() == null) {
s1.pop();
} else if (s1.peek() == !null) {
s1.pop();
s2.push();
counter++;
}
for (counter=10;counter>s1.size(); counter--){
}
s2.pop();
s1.push();
}
}
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 S: [2, null, null, 4, 6, null]"
System.out.println("stack s2: " + s2);
// prints: "stack X: [7, 9]"
compress(S1, s2);
System.out.println("stack S1: " + S1);
// should print: "stack S: [2, 4, 6]"
System.out.println("stack s2: " + s2);
// should print: "stack X: [7, 9]"
}
}
Eclipseは、peek()メソッドとpush()メソッドの両方でエラーを出します。pop()メソッドを許可します。これらのメソッドが継承されていることは私の理解でしたか?どんな助けでも大歓迎です!