import Stack.Array.Stack;
// import Stack.List.Stack;
public class Main {
private static void testOverflow() {
try {
while (true) {
System.
out.
printf("現在要素数は%dです。オブジェクト%sを追加します。%n", stack.
size(), o
); // これも課題としては不要 stack.push(o);
}
} catch (Stack.
StackException e
) { System.
err.
printf("スタックのサイズは%dです。これ以上要素は追加できません。%n", stack.
size()); }
}
private static void testUnderflow() {
try {
/* // 空のスタックからポップするだけの課題なので要らないけれど
// stack.pop();の替わりにコメント内の方にすると、追加してから過剰に削除する
for (int i = 0; i < 10; i++) {
Object o = new Object();
System.out.printf("現在要素数は%dです。オブジェクト%sを追加します。%n", stack.size(), o);
stack.push(o);
}
System.out.printf("現在要素数は%dです。%n", stack.size());
while (true) {
System.out.printf("オブジェクト%sが取り出されました。", stack.pop());
System.out.printf("現在要素数は%dです。%n", stack.size());
}
*/
stack.pop();
} catch (Stack.
StackException e
) { System.
err.
println("スタックは空です。要素を取り出すことはできません。"); }
}
public static void main
(String[] args
) { System.
out.
println("--- testOverflow"); testOverflow();
System.
out.
println("--- testUnderflow"); testUnderflow();
}
}
aW1wb3J0IFN0YWNrLkFycmF5LlN0YWNrOwovLyBpbXBvcnQgU3RhY2suTGlzdC5TdGFjazsKCnB1YmxpYyBjbGFzcyBNYWluIHsKICAgIHByaXZhdGUgc3RhdGljIHZvaWQgdGVzdE92ZXJmbG93KCkgewogICAgICAgIFN0YWNrIHN0YWNrID0gbmV3IFN0YWNrKCk7CiAgICAgICAgdHJ5IHsKICAgICAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgICAgICAgIE9iamVjdCBvID0gbmV3IE9iamVjdCgpOwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIuePvuWcqOimgee0oOaVsOOBryVk44Gn44GZ44CC44Kq44OW44K444Kn44Kv44OIJXPjgpLov73liqDjgZfjgb7jgZnjgIIlbiIsIHN0YWNrLnNpemUoKSwgbyk7IC8vIOOBk+OCjOOCguiqsumhjOOBqOOBl+OBpuOBr+S4jeimgQogICAgICAgICAgICAgICAgc3RhY2sucHVzaChvKTsKICAgICAgICAgICAgfQogICAgICAgIH0gY2F0Y2ggKFN0YWNrLlN0YWNrRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgU3lzdGVtLmVyci5wcmludGYoIuOCueOCv+ODg+OCr+OBruOCteOCpOOCuuOBryVk44Gn44GZ44CC44GT44KM5Lul5LiK6KaB57Sg44Gv6L+95Yqg44Gn44GN44G+44Gb44KT44CCJW4iLCBzdGFjay5zaXplKCkpOwogICAgICAgIH0KICAgIH0KCiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIHRlc3RVbmRlcmZsb3coKSB7CiAgICAgICAgU3RhY2sgc3RhY2sgPSBuZXcgU3RhY2soKTsKICAgICAgICB0cnkgewogICAgICAgICAgICAvKiAvLyDnqbrjga7jgrnjgr/jg4Pjgq/jgYvjgonjg53jg4Pjg5fjgZnjgovjgaDjgZHjga7oqrLpoYzjgarjga7jgafopoHjgonjgarjgYTjgZHjgozjgakKICAgICAgICAgICAgICAgLy8gc3RhY2sucG9wKCk744Gu5pu/44KP44KK44Gr44Kz44Oh44Oz44OI5YaF44Gu5pa544Gr44GZ44KL44Go44CB6L+95Yqg44GX44Gm44GL44KJ6YGO5Ymw44Gr5YmK6Zmk44GZ44KLCiAgICAgICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgMTA7IGkrKykgewogICAgICAgICAgICAgICAgT2JqZWN0IG8gPSBuZXcgT2JqZWN0KCk7CiAgICAgICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50Zigi54++5Zyo6KaB57Sg5pWw44GvJWTjgafjgZnjgILjgqrjg5bjgrjjgqfjgq/jg4glc+OCkui/veWKoOOBl+OBvuOBmeOAgiVuIiwgc3RhY2suc2l6ZSgpLCBvKTsKICAgICAgICAgICAgICAgIHN0YWNrLnB1c2gobyk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIuePvuWcqOimgee0oOaVsOOBryVk44Gn44GZ44CCJW4iLCBzdGFjay5zaXplKCkpOwogICAgICAgICAgICB3aGlsZSAodHJ1ZSkgewogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIuOCquODluOCuOOCp+OCr+ODiCVz44GM5Y+W44KK5Ye644GV44KM44G+44GX44Gf44CCIiwgc3RhY2sucG9wKCkpOwogICAgICAgICAgICAgICAgU3lzdGVtLm91dC5wcmludGYoIuePvuWcqOimgee0oOaVsOOBryVk44Gn44GZ44CCJW4iLCBzdGFjay5zaXplKCkpOwogICAgICAgICAgICB9CiAgICAgICAgICAgICovCiAgICAgICAgICAgIHN0YWNrLnBvcCgpOwogICAgICAgIH0gY2F0Y2ggKFN0YWNrLlN0YWNrRXhjZXB0aW9uIGUpIHsKICAgICAgICAgICAgU3lzdGVtLmVyci5wcmludGxuKCLjgrnjgr/jg4Pjgq/jga/nqbrjgafjgZnjgILopoHntKDjgpLlj5bjgorlh7rjgZnjgZPjgajjga/jgafjgY3jgb7jgZvjgpPjgIIiKTsKICAgICAgICB9CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiLS0tIHRlc3RPdmVyZmxvdyIpOwogICAgICAgIHRlc3RPdmVyZmxvdygpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbigiLS0tIHRlc3RVbmRlcmZsb3ciKTsKICAgICAgICB0ZXN0VW5kZXJmbG93KCk7CiAgICB9Cn0K