본문 바로가기

2023.11.21-2024.05.31

240103 JAVA Stack

 * 선입선출 First In First Out "먼저 넣은 객체가 먼저 빠져나가는 구조"
 * 후입선출 Last In First Out "나중에 넣은 객체가 먼저 빠져나가는 구조"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.poseidon.coll;
import java.util.Stack;
 
class Coin{
    private int value;
    
    public Coin(int value) {
        this.value = value;
    }
    
    public int getValue() {
        return value;
    }
}
public class Stack01 {
    public static void main(String[]args) {
        Stack<Coin> coinBox = new Stack<Coin>();
        coinBox.push(new Coin(100));
        coinBox.push(new Coin(50));
        coinBox.push(new Coin(500));
        coinBox.push(new Coin(10));
        
        while(!coinBox.isEmpty()) {
            Coin coin = coinBox.pop();
            System.out.println("꺼내온 동전 :"+ coin.getValue());
        }
    
    }
 
}
 
cs

꺼내온 동전 :10

꺼내온 동전 :500

꺼내온 동전 :50

꺼내온 동전 :100

'2023.11.21-2024.05.31' 카테고리의 다른 글

240102 HTML index.jsp  (0) 2024.01.03
240103 JAVA IO(OutputStream,InputStream)  (1) 2024.01.03
240103 JAVA Inner  (0) 2024.01.03
240103 JAVA Queue01  (0) 2024.01.03
240103 JAVA Thread  (2) 2024.01.03