๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–‹๏ธ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[๋ฉ”์„œ๋“œ] ArrayDeque ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ

by OR15A 2023. 11. 30.
ArrayDeque ํด๋ž˜์Šค
  • ArrayDeque ๋Š” ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๋”๋ธ” ์—”๋“œ ํ(double-ended queue)๋ฅผ ๊ตฌํ˜„ํ•œ Java ํด๋ž˜์Šค
  • ArrayDeque ๋Š” Queue ์ธํ„ฐํŽ˜์ด์Šค์™€ Deque ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉฐ, ์Šคํƒ๊ณผ ํ์˜ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์ œ๊ณตํ•จ

 

์ƒ์„ฑ์ž
  1. ArrayDeque():
    • ๋นˆ ArrayDeque๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  2. ArrayDeque(int numElements):
    • ์ง€์ •๋œ ์ดˆ๊ธฐ ์šฉ๋Ÿ‰์„ ๊ฐ€์ง„ ArrayDeque๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  3. ArrayDeque(Collection<? extends E> c):
    • ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ArrayDeque๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

์ฃผ์š” ๋ฉ”์„œ๋“œ
  1. void addFirst(E e):
    • ํ์˜ ์•ž์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  2. void addLast(E e):
    • ํ์˜ ๋’ค์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  3. boolean addAll(Collection<? extends E> c):
    • ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  4. boolean offerFirst(E e):
    • ํ์˜ ์•ž์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  5. boolean offerLast(E e):
    • ํ์˜ ๋’ค์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  6. E removeFirst():
    • ํ์˜ ์•ž์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  7. E removeLast():
    • ํ์˜ ๋’ค์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  8. E pollFirst():
    • ํ์˜ ์•ž์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  9. E pollLast():
    • ํ์˜ ๋’ค์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  10. E getFirst():
    • ํ์˜ ์•ž์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
  11. E getLast():
    • ํ์˜ ๋’ค์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
  12. E peekFirst():
    • ํ์˜ ์•ž์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์œผ๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  13. E peekLast():
    • ํ์˜ ๋’ค์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์œผ๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  14. boolean removeFirstOccurrence(Object o):
    • ์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ์ผ์น˜ํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  15. boolean removeLastOccurrence(Object o):
    • ์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ์ผ์น˜ํ•˜๋Š” ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

 


 

Queue ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์„œ๋“œ

  1. boolean add(E e):
    • ํ์˜ ๋’ค์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  2. boolean offer(E e):
    • ํ์˜ ๋’ค์ชฝ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  3. E remove():
    • ํ์˜ ์•ž์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  4. E poll():
    • ํ์˜ ์•ž์ชฝ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  5. E element():
    • ํ์˜ ์•ž์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.
  6. E peek():
    • ํ์˜ ์•ž์ชฝ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ ์ œ๊ฑฐํ•˜์ง€๋Š” ์•Š์œผ๋ฉฐ, ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

Stack ๋ฉ”์„œ๋“œ

  1. void push(E e):
    • ์Šคํƒ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. addFirst์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
  2. E pop():
    • ์Šคํƒ์—์„œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. removeFirst์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

 

 

Collection ์ธํ„ฐํŽ˜์ด์Šค ๋ฉ”์„œ๋“œ

  1. int size():
    • ํ์— ์žˆ๋Š” ์š”์†Œ์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  2. boolean isEmpty():
    • ํ๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  3. Iterator<E> iterator():
    • ํ์˜ ์š”์†Œ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  4. Iterator<E> descendingIterator():
    • ํ์˜ ์š”์†Œ์— ๋Œ€ํ•œ ์—ญ๋ฐฉํ–ฅ ๋ฐ˜๋ณต์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  5. Spliterator<E> spliterator():
    • ํ์˜ ์š”์†Œ์— ๋Œ€ํ•œ ์Šคํ”Œ๋ฆฌํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  6. void forEach(Consumer<? super E> action):
    • ํ์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  7. boolean removeIf(Predicate<? super E> filter):
    • ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋ฅผ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  8. boolean removeAll(Collection<?> c):
    • ์ง€์ •๋œ ์ปฌ๋ ‰์…˜์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  9. boolean retainAll(Collection<?> c):
    • ์ง€์ •๋œ ์ปฌ๋ ‰์…˜์— ์—†๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  10. boolean contains(Object o):
    • ํ๊ฐ€ ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  11. boolean remove(Object o):
    • ์ฃผ์–ด์ง„ ๊ฐ์ฒด์™€ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ํ์—์„œ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  12. void clear():
    • ํ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  13. Object[] toArray():
    • ํ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

๋‚˜๋จธ์ง€ ์ฃผ์š” ๊ณต๊ฐœ ๋ฉ”์„œ๋“œ

  1. <T> T[] toArray(T[] a):
    • ์„ค๋ช…: ํ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฉ”์„œ๋“œ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ํƒ€์ž…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
  2. private <T> T[] toArray(Class<T[]> klazz):
    • ์„ค๋ช…: ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์„œ๋“œ๋กœ, ํŠน์ • ํด๋ž˜์Šค ํƒ€์ž…์˜ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์—ฌ ํ์˜ ์š”์†Œ๋ฅผ ์ด ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  3. private void writeObject(java.io.ObjectOutputStream s):
    • ์„ค๋ช…: ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ArrayDeque์˜ ์ƒํƒœ๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  4. private void readObject(java.io.ObjectInputStream s):
    • ์„ค๋ช…: ์—ญ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ArrayDeque์˜ ์ƒํƒœ๋ฅผ ์ฝ์–ด๋“ค์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  5. void checkInvariants():
    • ์„ค๋ช…: ArrayDeque์˜ ๋‚ด๋ถ€ ์ƒํƒœ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

๋‚ด๋ถ€์  ์‚ฌ์šฉ ๋ฉ”์„œ๋“œ ๋ฐ ๊ตฌํ˜„

ArrayDeque์—๋Š” transient Object[] elements, transient int head, transient int tail ๋“ฑ์˜ ํ•„๋“œ์™€ grow, newCapacity, copyElements, circularClear ๋“ฑ์˜ ๋‚ด๋ถ€ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ArrayDeque์˜ ๋‚ด๋ถ€ ๊ตฌํ˜„๊ณผ ๊ด€๋ จ์ด ์žˆ์œผ๋ฉฐ, ArrayDeque์˜ ํฌ๊ธฐ ์กฐ์ •, ์š”์†Œ ๋ณต์‚ฌ, ๋ฐฐ์—ด ์ˆœํ™˜ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

transient Object[] elements; transient int head; transient int tail; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; private void grow(int needed) { private int newCapacity(int needed, int jump) { public ArrayDeque() public ArrayDeque(int numElements) public ArrayDeque(Collection<? extends E> c) static final int inc(int i, int modulus) static final int dec(int i, int modulus) static final int inc(int i, int distance, int modulus) static final int sub(int i, int j, int modulus) public void addFirst(E e) public void addLast(E e) public boolean addAll(Collection<? extends E> c) private void copyElements(Collection<? extends E> c) public boolean offerFirst(E e) public boolean offerLast(E e) public E removeFirst() public E removeLast() public E pollFirst() public E pollLast() public E getFirst() public E getLast() public E peekFirst() public E peekLast() public boolean removeFirstOccurrence(Object o) public boolean removeLastOccurrence(Object o) [Queue methods ] public boolean add(E e) public boolean offer(E e) public E remove() public E poll() public E element() public E peek() [Stack methods] public void push(E e) public E pop() boolean delete(int i) [Collection Methods *** ] public int size() public boolean isEmpty() public Iterator<E> iterator() public Iterator<E> descendingIterator() private class DeqIterator public Spliterator<E> spliterator() final class DeqSpliterator public void forEach(Consumer<? super E> action) public boolean removeIf(Predicate<? super E> filter) public boolean removeAll(Collection<?> c) public boolean retainAll(Collection<?> c) private boolean bulkRemove(Predicate<? super E> filter) private static long[] nBits(int n) private static void setBit(long[] bits, int i) private static boolean isClear(long[] bits, int i) private boolean bulkRemoveModified public boolean contains(Object o) public boolean remove(Object o) public void clear() private static void circularClear(Object[] es, int i, int end) public Object[] toArray() private <T> T[] toArray(Class<T[]> klazz) private void writeObject(java.io.ObjectOutputStream s) private void readObject(java.io.ObjectInputStream s) void checkInvariants()