Files
hello-algo/ja/codes/java/chapter_stack_and_queue/array_stack.java
T
Ikko Eltociear Ashimine 954c45864b docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
2025-10-17 05:04:43 +08:00

84 lines
2.2 KiB
Java

/**
* File: array_stack.java
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
package chapter_stack_and_queue;
import java.util.*;
/* 配列に基づくスタッククラス */
class ArrayStack {
private ArrayList<Integer> stack;
public ArrayStack() {
// リスト(動的配列)を初期化
stack = new ArrayList<>();
}
/* スタックの長さを取得 */
public int size() {
return stack.size();
}
/* スタックが空かどうかを判定 */
public boolean isEmpty() {
return size() == 0;
}
/* プッシュ */
public void push(int num) {
stack.add(num);
}
/* ポップ */
public int pop() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.remove(size() - 1);
}
/* スタックトップ要素にアクセス */
public int peek() {
if (isEmpty())
throw new IndexOutOfBoundsException();
return stack.get(size() - 1);
}
/* List を Array に変換して返す */
public Object[] toArray() {
return stack.toArray();
}
}
public class array_stack {
public static void main(String[] args) {
/* スタックを初期化 */
ArrayStack stack = new ArrayStack();
/* 要素をプッシュ */
stack.push(1);
stack.push(3);
stack.push(2);
stack.push(5);
stack.push(4);
System.out.println("スタック stack = " + Arrays.toString(stack.toArray()));
/* スタックトップ要素にアクセス */
int peek = stack.peek();
System.out.println("スタックトップ要素 peek = " + peek);
/* 要素をポップ */
int pop = stack.pop();
System.out.println("ポップした要素 = " + pop + "、ポップ後 " + Arrays.toString(stack.toArray()));
/* スタックの長さを取得 */
int size = stack.size();
System.out.println("スタックの長さ size = " + size);
/* 空かどうかを判定 */
boolean isEmpty = stack.isEmpty();
System.out.println("スタックが空か = " + isEmpty);
}
}