1οΈβ£ ArrayDeque.
Javaμμ βArrayDeque
β λ βjava.util
β ν¨ν€μ§μ μνλ ν΄λμ€μ΄λ©°, ν(Queue)μ λ±(Deque)μ κΈ°λ₯μ λͺ¨λ μ§μνλ λ°°μ΄ κΈ°λ°μ μλ£ κ΅¬μ‘°μ
λλ€.
βArrayDeque
β λ βDeque
β μΈν°νμ΄μ€λ₯Ό ꡬννλ©°, κ·ΈκΈ°κ° κ°λ³μ μΈ λ°°μ΄μ μ¬μ©νμ¬ μμλ₯Ό μ μ₯ν©λλ€.
2οΈβ£ μ£Όμ νΉμ§.
-
μ΄μ€ λ ν : μμͺ½ λμμ μμλ₯Ό μΆκ°νκ³ μ κ±°ν μ μμ΅λλ€.
-
ν¬κΈ° μ‘°μ : νμμ λ°λΌ λ΄λΆ λ°°μ΄μ ν¬κΈ°λ₯Ό μλμΌλ‘ μ‘°μ ν©λλ€.
-
μ€ν λ° νλ‘ μ¬μ© κ°λ₯ : β
ArrayDeque
β λ μ€ν(LIFO, Last In First Out)κ³Ό ν(FIFO, First In First Out) λͺ¨λλ‘ μ¬μ©ν μ μμ΅λλ€. -
λΉλκΈ°μ : β
ArrayDeque
β λ λΉλκΈ°μ μΌλ‘ λμνλ―λ‘ λκΈ°νλ νκ²½μμ μμ νμ§ μμ΅λλ€.
3οΈβ£ μ£Όμ λ©μλ.
μ½μ μ°μ°.
-
β
addFirst(E e)
β : μ§μ λ μμλ₯Ό λ±μ μμͺ½μ μΆκ°ν©λλ€. -
β
addLast(E e)
β : μ§μ λ μμλ₯Ό λ±μ λ€μͺ½μ μΆκ°ν©λλ€. -
β
offerFirst(E e)
β : μ§μ λ μμλ₯Ό λ±μ μμͺ½μ μΆκ°ν©λλ€. -
β
offerLast(E e)
β : μ§μ λ μμλ₯Ό λ±μ λ€μͺ½μ μΆκ°ν©λλ€.
μμ μ°μ°.
-
β
removeFirst()
β : λ±μ μμͺ½μμ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€. -
β
removeLast()
β : λ±μ λ€μͺ½μμ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€. -
β
pollFirst()
β : λ±μ μμͺ½μμ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€. -
β
pollLast()
β : λ±μ λ€μͺ½μμ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€.
μ‘°ν μ°μ°.
-
β
getFirst()
β : λ±μ μμͺ½μ μλ μμλ₯Ό λ°νν©λλ€. -
β
getLast()
β : λ±μ λ€μͺ½μ μλ μμλ₯Ό λ°νν©λλ€. -
β
peekFirst()
β : λ±μ μμͺ½μ μλ μμλ₯Ό λ°νν©λλ€. -
β
peekLast()
β : λ±μ λ€μͺ½μ μλ μμλ₯Ό λ°νν©λλ€.
μ€ν μ°μ°.
-
β
push(E e)
β : μ€νμ 맨 μμ μμλ₯Ό μΆκ°ν©λλ€.(LIFO, Last In First Out) -
β
pop(E e)
β : μ€νμ 맨 μμ μλ μμλ₯Ό μ κ±°νκ³ λ°νν©λλ€.(LIFO, Last In First Out)
4οΈβ£ μκ° λ³΅μ‘λ.
-
μ½μ κ³Ό μμ μ°μ° : β
addFirst
β, βaddLast
β, βremoveFirst
β, βremoveLast
β, βofferFirst
β, βofferLast
β, βpollFirst
β, βpollLast
β, λ±μ μ°μ°μ νκ· μ μΌλ‘ O(1)μ λλ€. -
μ‘°ν μ°μ° : β
getFirst
β, βgetLast
β, βpeekFirst
β, βpeekLast
β λ±μ μ°μ°μ O(1)μ λλ€. -
ν¬κΈ° μ‘°μ : λ² μ΄μ ν¬κΈ°κ° κ°λ μ°Όμ λ ν¬κΈ°λ₯Ό λ λ°°λ‘ λ리거λ μ€μ΄λ μμ μ O(n) μκ°μ΄ 걸리μ§λ§, μ΄λ λλ¬Όκ² λ°μνλ―λ‘ νκ· μ μΌλ‘λ O(1)λ‘ κ°μ£Όν©λλ€. (amortized O(1)).
5οΈβ£ μμ μ½λ
μλμ μ½λλ βArrayDeque
β λ₯Ό μ¬μ©ν μμ μ½λμ
λλ€.
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeExample {
public static void main(String[] args) {
// ArrayDequeλ‘ Deque μμ±
Deque<Integer> deque = new ArrayDeque<>();
// μμ μ½μ
System.out.println("=== μμ μ½μ
===");
deque.addFirst(1);
deque.addLast(2);
deque.offerFirst(0);
deque.offerLast(3);
System.out.println(deque);
System.out.println();
// μμ μ‘°ν
System.out.println("=== μμ μ‘°ν ===");
System.out.println("First element: " + deque.getFirst());
System.out.println("Last element: " + deque.getLast());
System.out.println("Peek first element: " + deque.peekFirst());
System.out.println("Peek last element: " + deque.peekLast());
System.out.println();
// μμ μμ
System.out.println("=== μμ μμ ===");
System.out.println("Removed first element: " + deque.removeFirst());
System.out.println("Removed last element: " + deque.removeLast());
System.out.println("Poll first element: " + deque.pollFirst());
System.out.println("Poll last element: " + deque.pollLast());
System.out.println();
// λ±μ ν¬κΈ°μ λΉμ΄ μλμ§ μ¬λΆ νμΈ
System.out.println("=== λ±μ ν¬κΈ°μ λΉμ΄ μλμ§ μ¬λΆ νμΈ ===");
System.out.println("Deque size: " + deque.size());
System.out.println("Is deque empty? " + deque.isEmpty());
System.out.println();
// μ€ν μ°μ°
System.out.println("=== μ€ν μ°μ° ===");
deque.push(4);
System.out.println("Pushed element: " + deque.peekFirst());
System.out.println("Popped element: " + deque.pop());
}
}
/*
=== μΆλ ₯ ===
=== μμ μ½μ
===
[0, 1, 2, 3]
=== μμ μ‘°ν ===
First element: 0
Last element: 3
Peek first element: 0
Peek last element: 3
=== μμ μμ ===
Removed first element: 0
Removed last element: 3
Poll first element: 1
Poll last element: 2
=== λ±μ ν¬κΈ°μ λΉμ΄ μλμ§ μ¬λΆ νμΈ ===
Deque size: 0
Is deque empty? true
=== μ€ν μ°μ° ===
Pushed element: 4
Popped element: 4
*/