๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

WHITESHIP JAVA LIVE STUDY

๐Ÿญ ๋ฐ˜๋ณต๋ฌธ(for each)

for each ๋Š” J2SE 5.0(Java Platform, Standard Edition) ๋ถ€ํ„ฐ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.

for each๋ผ๋Š” ํ‚ค์›Œ๋“œ๊ฐ€ ๋”ฐ๋กœ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  ๋™์ผํ•œ for๋ฅผ ์ด์šฉํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์กฐ๊ฑด์‹ ๋ถ€๋ถ„์ด ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค. ๋ณดํ†ต ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ for each ๋ผ๊ณ  ๋งŽ์ด ํ•˜๋ฏ€๋กœ ์ž๋ฐ”์—์„œ๋„ ๋ณดํ†ต for each ๋ฌธ์ด๋ผ๊ณ  ๋งํ•œ๋‹ค.

๋ฌธ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

for (type var: iterate) {
    body-of-loop
}

์œ„ iterate(: ๋ฐ˜๋ณตํ•˜๋‹ค) ๋Š” ๋ฃจํ”„๋ฅผ ๋Œ๋ฆด ๊ฐ์ฒด์ด๊ณ  iterate ๊ฐ์ฒด์—์„œ ํ•œ๊ฐœ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ var ์— ๋Œ€์ž…๋˜์–ด for ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค.

iterate ๋ถ€๋ถ„์— ๋“ค์–ด๊ฐ€๋Š” ํƒ€์ž…์€ ๋ฃจํ”„๋ฅผ ๋Œ๋ฆด ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ์ธ ๋ฐฐ์—ด ๋ฐ ArrayList ๋“ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

์˜ˆ)

for ๋ฌธ

String [] numbers = {"one", "two", "three"};
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
}

for each ๊ตฌ์กฐ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

String [] numbers = {"one", "two", "three"};
for (String number: numbers) {
    System.out.println(number);
}
ArrayList<String> numbers = new ArrayList<String>();
numbers.add("one");
numbers.add("two");
numbers.add("three");

for (String number: numbers) {
    System.out.println(number);
}

๋‹จ, foreach ๋ฌธ์€ ๋”ฐ๋กœ ๋ฐ˜๋ณตํšŒ์ˆ˜๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ฃผ๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ณ , 1์Šคํƒญ์”ฉ ์ˆœ์ฐจ์ ์œผ๋กœ ๋ฐ˜๋ณต๋ ๋•Œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ œ์•ฝ์ด ์žˆ๋‹ค.

๐Ÿ–‡ LinkedList

LinkedList๋ฅผ ์•Œ์•„๋ณด๊ธฐ์— ์•ž์„œ ์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์ž.

 

์ปฌ๋ ‰์…˜ ํ”„๋ ˆ์ž„์›Œํฌ

๋ฐ์ดํ„ฐ ๊ตฐ์„ ์ €์žฅํ•˜๋Š” ํด๋ž˜์Šค๋“ค์„ ํ‘œ์ค€ํ™”ํ•œ ์„ค๊ณ„ ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฐ์„ ๋‹ค๋ฃจ๊ณ  ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ๋‹จ์ผํ™”๋œ ๊ตฌ์กฐ(architecture)๋ฅผ ๋œปํ•œ๋‹ค.

์ปฌ๋ ‰์…˜์€ ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ, ๋ฐ์ดํ„ฐ ๊ทธ๋ฃน์„ ์˜ํžˆ๋งˆ๊ณ ,
ํ”„๋ ˆ์ž„์›Œํฌ๋Š” ํ‘œ์ค€ํ™”๋œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹์„ ์˜๋ฏธํ•œ๋‹ค.

์ปฌ๋ ‰์…˜(๋ฐ์ดํ„ฐ ๊ทธ๋ฃน, ๋‹ค์ˆ˜์˜ ๋ฐ์ดํ„ฐ)๋Š” ํฌ๊ฒŒ ์„ธ๊ฐ€์ง€ ์ข…๋ฅ˜๋กœ ๋‚˜๋‰œ๋‹ค.
์ด ์„ธ๊ฐ€์ง€ ์ปฌ๋ ‰์…˜์„ ๋‹ค๋ฃจ๋Š” ๋ฐ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ 3๊ฐ€์ง€ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์กด์žฌํ•˜๋Š”๋ฐ, ๋ฐ”๋กœ
List, Set, Map์ด๋‹ค.

์ธํ„ฐํŽ˜์ด์Šค ํŠน์ง•
List ์ˆœ์„œ : O
๋ฐ์ดํ„ฐ ์ค‘๋ณต : O
์˜ˆ) ๋Œ€๊ธฐ์ž ๋ช…๋‹จ
๊ตฌํ˜„ ํด๋ž˜์Šค: ArrayList, LinkedList, Stack, Vector ๋“ฑ
Set ์ˆœ์„œ : X
๋ฐ์ดํ„ฐ ์ค‘๋ณต : X
์˜ˆ) ์–‘์˜ ์ •์ˆ˜ ์ง‘ํ•ฉ, ์†Œ์ˆ˜์˜ ์ง‘ํ•ฉ
๊ตฌํ˜„ ํด๋ž˜์Šค: HashSet, TreeSet ๋“ฑ
Map ํ‚ค(key)์™€ ๊ฐ’(value)์˜ ์Œ(pair)๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ.
์ˆœ์„œ : X
ํ‚ค์˜ ์ค‘๋ณต : X
๊ฐ’์˜ ์ค‘๋ณต : O
์˜ˆ) ์šฐํŽธ๋ฒˆํ˜ธ, ์ง€์—ญ๋ฒˆํ˜ธ(์ „ํ™”๋ฒˆํ˜ธ)
๊ตฌํ˜„ ํด๋ž˜์Šค : HashMap, TreeMap, Hashtable, Properties ๋“ฑ

์œ„ ํ‘œ์˜ List ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฐ”๋กœ LinkedList์ด๋‹ค.

๐Ÿ’ก ์ฐธ๊ณ 
List ์™€ Set ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ณตํ†ต๋œ ๋ถ€๋ถ„์ด ๋งŽ์•„ ๊ทธ ๊ณตํ†ต๋œ ๋ถ€๋ถ„์„ ๋ชจ์•„ ๋งŒ๋“  Collection ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ๋‹ค.
์ฆ‰, List์™€ Set์˜ ์กฐ์ƒ์œผ๋กœ Collection ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

๋”๋ณด๊ธฐ

Collection ์ธํ„ฐํŽ˜์ด์Šค์— ์ •์˜๋œ ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ

๋ฉ”์„œ๋“œ ์„ค๋ช…
boolean add(Object o) โญ
boolean addAll(Collection c)
์ง€์ •๋œ ๊ฐ์ฒด(o) ๋˜๋Š” Collection(c)์˜ ๊ฐ์ฒด๋“ค์„ Collction ์— ์ถ”๊ฐ€ํ•œ๋‹ค.
void clear() Collection์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
boolean contains(Object o)
boolean containsAll(Collection c)
์ง€์ •๋œ ๊ฐ์ฒด(o) ๋˜๋Š”  Collection์˜ ๊ฐ์ฒด๋“ค์ด Collection์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
boolean equals(Object o) โญ ๋™์ผํ•œ Collection ์ธ์ง€ ๋น„๊ตํ•œ๋‹ค.
int hashCode() Collection์˜ hash code๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean isEmpty() โญ Collection์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
Iterator iterator() Collection์˜ ๐Ÿ“ขIterator๋ฅผ ์–ป์–ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean remove(Object o) ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.
boolean removeAll(Collection c) ์ง€์ •๋œ Collection์— ํฌํ•จ๋œ ๊ฐ์ฒด๋“ค์„ ์‚ญ์ œํ•œ๋‹ค.
boolean retainAll(Collection c) ์ง€์ •๋œ Colection์— ํฌํ•จ๋œ ๊ฐ์ฒด๋งŒ์„ ๋‚จ๊ธฐ๊ณ  ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์€ Collection์—์„œ ์‚ญ์ œํ•œ๋‹ค. ์ด ์ž‘์—…์œผ๋กœ ์ธํ•ด Collection์— ๋ณ€ํ™”๊ฐ€ ์žˆ์œผ๋ฉด true๋ฅผ ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
int size() โญ Collection์— ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
Object[] toArray() Collection์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด ๋ฐฐ์—ด(Object[])๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
OBject[] toArray(Object[] a) ์ง€์ •๋œ ๋ฐฐ์—ด์— Collection์˜ ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ“ข Iterator

Iterator๋Š” ArrayList, HashSet ์ฒ˜๋Ÿผ ์ปฌ๋ ‰์…˜์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ฒด์ด๋‹ค.
์ž์„ธํžˆ

 

ArrayList

  • ์ˆœ์„œ O
  • ์ค‘๋ณต O
  • List ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๊ตฌํ˜„๋จ
  • Object ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•œ๋‹ค.
  • ๋‹จ์ 
    • ์šฉ๋Ÿ‰์„ ๋ณ€๊ฒฝํ•ด์•ผํ•  ๋•Œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ ํ›„ ๊ธฐ์กด์˜ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๋ฐฐ์—ด๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ๊ตฌ์กฐ๋ผ์„œ ํšจ์œจ์ด ๋–จ์–ด์ง„๋‹ค
๐Ÿ’ก ์ฐธ๊ณ ๋กœ ArrayList๋Š” ๋ฐฐ์—ด(Array)์™€ ๋‹ค๋ฅด๋‹ค.

์ž๋ฃŒ๊ตฌ์กฐ Array ArrayList
์‚ฌ์ด์ฆˆ ์ดˆ๊ธฐํ™” ์‹œ ๊ณ ์ •
int[] myArray = new int[6];
์ดˆ๊ธฐํ™” ์‹œ ์‚ฌ์ด์ฆˆ๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š์Œ. ์œ ๋™์ 
ArrayList<Integer> myArrayList = new ArrayList<>();
์†๋„ ์ดˆ๊ธฐํ™” ์‹œ ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น๋˜์–ด ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค. ์ถ”๊ฐ€ ์‹œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌํ• ๋‹นํ•˜์—ฌ ์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค
๋ณ€๊ฒฝ ์‚ฌ์ด์ฆˆ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ ์ถ”๊ฐ€, ์‚ญ์ œ ๊ฐ€๋Šฅ
add(), remove()
๋‹ค์ฐจ์› ๊ฐ€๋Šฅ
int[][] multiArray = new int[3][3];
๋ถˆ๊ฐ€๋Šฅ

 

ArrayList์˜ ๋Œ€ํ‘œ์ ์ธ ์ƒ์„ฑ์ž/๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์„ค๋ช…
ArrayList() ํฌ๊ธฐ๊ฐ€ 10์ธ ArrayList๋ฅผ ์ƒ์„ฑ
ArrayList(Collection c) โญ ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์ด ์ €์žฅ๋œ ArrayList๋ฅผ ์ƒ์„ฑ
ArrayList(int initialCapacity) โญ ์ง€์ •๋œ ์ดˆ๊ธฐ์šฉ๋Ÿ‰์„ ๊ฐ–๋Š” ArrayList๋ฅผ ์ƒ์„ฑ (capacity: ์šฉ๋Ÿ‰, ๋Šฅ๋ ฅ)
boolean add(Object o) โญ ArrayList์˜ ๋งˆ์ง€๋ง‰์— ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€. ์„ฑ๊ณตํ•˜๋ฉด true
void add(int index, Object element) ์ง€์ •๋œ ์œ„์น˜(index)์— ๊ฐ์ฒด๋ฅผ ์ €์žฅ
boolean addAll(Collection c) ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.
boolean addAll(int index, Collection c) โญ ์ง€์ •๋œ ์œ„์น˜๋ถ€ํ„ฐ ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•œ๋‹ค.
void clear() ArrayList๋ฅผ ์™„์ „ํžˆ ๋น„์šด๋‹ค.
Object clone() ArrayList๋ฅผ ๋ณต์ œํ•œ๋‹ค.
boolean contains(Object o) โญ ์ง€์ •๋œ ๊ฐ์ฒด(o)๊ฐ€ ArrayList์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
void ensureCapacity(int minCapacity) ArrayList์˜ ์šฉ๋Ÿ‰์ด ์ตœ์†Œํ•œ minCapacity๊ฐ€ ๋˜๋„๋กํ•œ๋‹ค. (capacity: ์šฉ๋Ÿ‰, ๋Šฅ๋ ฅ)
Object get(int index) โญ ์ง€์ •๋œ ์œ„์น˜(index)์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
int indexOf(Object o) ์ง€์ •๋„๋‹ˆ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
boolean isEmpty() ArrayList๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
Iterator iterator() ArrayList์˜ Iterator๊ฐ์น˜๋ฅผ ๋ฐ˜ํ™˜
int lastIndexOf(Object o) ๊ฐ์ฒด(o)๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜๋ฅผ ๋๋ถ€ํ„ฐ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๊ฒ€์ƒ‰ํ•ด์„œ ๋ฐ˜ํ™˜
ListIterator listIterator() ArrayList์˜ ListIterator๋ฅผ ๋ฐ˜ํ™˜
ListIterator listIterator(int index) ArrayList์˜ ์ง€์ •๋œ ์œ„์น˜๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ListIterator๋ฅผ ๋ฐ˜ํ™˜
Object remove(int index) โญ ์ง€์ •๋œ ์œ„์น˜(index)์— ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
boolean remove(Object o) ์ง€์ •ํ•œ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.(์„ฑ๊ณตํ•˜๋ฉด true, ์‹คํŒจํ•˜๋ฉด false)
boolean removeAll(Collection c) ์ง€์ •ํ•œ ์ปฌ๋ ‰์…˜์— ์ €์žฅ๋œ ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฐ์ฒด๋“ค์„ ArrayList์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
boolean retainAll(Colleciton c) โญ Arraylist์— ์ €์žฅ๋œ ๊ฐ์ฒด ์ค‘์—์„œ ์ฃผ์–ด์ง„ ์ปฌ๋ ˆ์…˜๊ณผ ๊ณตํ†ต๋œ ๊ฒƒ๋“ค๋งŒ์„ ๋‚จ๊ธฐ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์‚ญ์ œํ•œ๋‹ค. (retain: ์œ ์ง€ํ•˜๋‹ค)
Object set(int index, Object element) ์ฃผ์–ด์ง„ ๊ฐ์ฒด(element)๋ฅผ ์ง€์ •๋œ ์œ„์น˜(index)์— ์ €์žฅํ•œ๋‹ค.
int size() โญ ArrayList์— ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
void sort(Comparator c) โญ ์ง€์ •๋œ ์ •๋ ฌ ๊ธฐ์ค€(c)์œผ๋กœ ArrayList๋ฅผ ์ •๋ ฌ
List subList(int fromIndex, int toIndex) โญ fromIndex๋ถ€ํ„ฐ toIndex์‚ฌ์ด์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Object[] toArray() ArrayList์— ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์„ ๊ฐ์ฒด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Object toArray(Object[] a) ArrayList์— ์ €์žฅ๋œ ๋ชจ๋“  ๊ฐ์ฒด๋“ค์„ ๊ฐ์ฒด ๋ฐฐ์—ด a์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
void trimToSize() ์šฉ๋Ÿ‰์„ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์ค„์ธ๋‹ค.(๋นˆ ๊ณต๊ฐ„์„ ์—†์•ค๋‹ค.)

 

ArrayList ์˜ˆ์ œ

package com.java.test;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList list1 = new ArrayList(10);
        list1.add(new Integer(5));
        list1.add(new Integer(4));
        list1.add(new Integer(2));
        list1.add(new Integer(0));
        list1.add(new Integer(1));
        list1.add(new Integer(3));

        ArrayList list2 = new ArrayList(list1.subList(1,4));
        print(list1, list2);

        Collections.sort(list1); // list1๊ณผ list2๋ฅผ ์ •๋ ฌํ•œ๋‹ค.
        Collections.sort(list2);
        print(list1, list2);

        System.out.println("list1.containsAll(list2) : " + list1.containsAll(list2));

        list2.add("B");
        list2.add("C");
        list2.add(3, "A");
        print(list1, list2);

        list2.set(3, "AA");
        print(list1, list2);

        // list1์—์„œ list2์™€ ๊ฒน์น˜๋Š” ๋ถ€๋ถ„๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์‚ญ์ œํ•œ๋‹ค.
        System.out.println("list1.retainAll(list2) : " + list1.retainAll(list2));

        print(list1, list2);

        // list2์—์„œ list1์— ํฌํ•จ๋œ ๊ฐ์ฒด๋“ค์„ ์‚ญ์ œํ•œ๋‹ค.
        // ๋งŒ์•ฝ ๋ณ€์ˆ˜ i๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ๊ฐ€๋ฉด์„œ ์‚ญ์ œํ•˜๋ฉด, ํ•œ ์š”์†Œ๊ฐ€ ์‚ญ์ œ๋  ๋•Œ๋งˆ๋‹ค
        // ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์šฐ๊ธฐ ์œ„ํ•ด ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์ด ์ž๋ฆฌ ์ด๋™์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ฌ๋ฐ”๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†๋‹ค.
        for(int i = list2.size() - 1 ; i >= 0 ; i--) {
            if(list1.contains(list2.get(i)))
                list2.remove(i);
        }

        print(list1, list1);
    }

    static void print(ArrayList list1, ArrayList list2) {
        System.out.println("list1 :" + list1);
        System.out.println("list2 :" + list2);
        System.out.println();
    }
}

ArrayList ์˜ˆ์ œ ๊ฒฐ๊ณผ ํ™”๋ฉด

LinkedList

  • ๋ฐฐ์—ด์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์ž๋ฃŒ๊ตฌ์กฐ
  • ๋ถˆ์—ฐ์†์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐ(link)ํ•œ ํ˜•ํƒœ
โœ ๋ฐฐ์—ด์˜ ๋‹จ์ 
1. ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค.
2. ๋น„์ˆœ์ฐจ์ ์ธ ๋ฐ์ดํ„ฐ์˜ ์ถ”๊ฐ€ ๋˜๋Š” ์‚ญ์ œ์— ์‹œ๊ฐ„์ด ๋งŽ์ด ๊ฑธ๋ฆฐ๋‹ค

๊ฐ ์š”์†Œ(node)๋“ค์€ ์ž์‹ ๊ณผ ์—ฐ๊ฒฐ๋œ ๋‹ค์Œ ์š”์†Œ์— ๋Œ€ํ•œ ์ฐธ์กฐ(์ฃผ์†Œ๊ฐ’)์™€ ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

ex) ์ฐธ์กฐ(์ฃผ์†Œ๊ฐ’) : 0x300 / ๋ฐ์ดํ„ฐ : 0

class Node {
    Node next; // ๋‹ค์Œ ์š”์†Œ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅ
    Object obj; // ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
}

 

LinkedList ์˜ ์‚ญ์ œ

์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์˜ ์ด์ „์š”์†Œ๊ฐ€ ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์˜ ๋‹ค์Œ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด๋œ๋‹ค. 

๋‹จ ํ•˜๋‚˜์˜ ์ฐธ์กฐ๋งŒ ๋ณ€๊ฒฝํ•˜๋ฉด ์‚ญ์ œ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์ด๋‹ค.  

linkedList์˜ ๋ฐ์ดํ„ฐ ์‚ญ์ œ

LinkedList ์˜ ์ถ”๊ฐ€

์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•œ ๋‹ค์Œ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ์œ„์น˜์˜ ์ด์ „ ์š”์†Œ์˜ ์ฐธ์กฐ๋ฅผ ์ƒˆ๋กœ์šด ์š”์†Œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ๊ณ ,

์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ๊ทธ ๋‹ค์Œ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๋ฏ€๋กœ ์ฒ˜๋ฆฌ์†๋„๊ฐ€ ๋งค์šฐ ๋น ๋ฅด๋‹ค.

linkedList์˜ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€

linkedList๋Š” ์ด๋™๋ฐฉํ–ฅ์ด ๋‹จ๋ฐฉํ–ฅ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ ์š”์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์€ ์‰ฝ์ง€๋งŒ ์ด์ „์š”์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ์€ ์–ด๋ ต๋‹ค.

์ด ์ ์„ ๋ณด์™„ํ•œ ๊ฒƒ์ด ๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ(์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ, doubly linked list)์ด๋‹ค.

 

๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ

๋‹จ์ˆœํžˆ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์— ์ฐธ์กฐ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•˜์—ฌ ๋‹ค์Œ ์š”์†Œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฟ ์•„๋‹ˆ๋ผ ์ด์ „ ์š”์†Œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ–ˆ์„ ๋ฟ, ๊ทธ ์™ธ์—๋Š” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™๋‹ค.

๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋Š” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์ ‘๊ทผ๊ณผ ์ด๋™์ด ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๋” ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

class Node {
    Node next; // ๋‹ค์Œ ์š”์†Œ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅ
    Node previous; // ์ด์ „ ์š”์†Œ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅ
    Object obj; // ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
}

๋”๋ธ”๋“œ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ

๋”๋ธ” ์จํ˜๋Ÿฌ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ

์ด์ค‘ ์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ.

๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ ‘๊ทผ์„ฑ์„ ๋ณด๋‹ค ํ–ฅ์ƒ ์‹œํ‚จ ๊ฒƒ

๋‹จ์ˆœํžˆ ๋”๋ธ”๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์„œ๋กœ ์—ฐ๊ฒฐ ์‹œํ‚จ ๊ฒƒ์ด๋‹ค.

๋”๋ธ” ์จํ˜๋Ÿฌ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ƒ์„ฑ์ž์™€ ๋ฉ”์„œ๋“œ

๋”๋ณด๊ธฐ
์ƒ์„ฑ์ž ๋˜๋Š” ๋ฉ”์„œ๋“œ ์„ค๋ช…
LinkedList() LinkedList ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
LinkedList(Colection c) ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์„ ํฌํ•จํ•˜๋Š” LinkedList ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
boolean add(Object o) ์ง€์ •๋œ ๊ฐ์ฒด(o)๋ฅผ LinkedList์˜ ๋์— ์ถ”๊ฐ€. ์ €์žฅ์— ์„ฑ๊ณตํ•˜๋ฉด true, ์‹คํŒจํ•˜๋ฉด false
void add(int index, Object element) ์ง€์ •๋œ ์œ„์น˜(index)์— ๊ฐ์ฒด(elemnt)๋ฅผ ์ถ”๊ฐ€
boolean addAll(Collection c) ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์— ํฌํ•จ๋œ ๋ชจ๋“  ์š”์†Œ๋ฅผ LinkedList์˜ ๋์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์„ฑ๊ณตํ•˜๋ฉด ture, ์‹คํŒจํ•˜๋ฉด false
boolean addAll(int index, Collection c) ์ง€์ •๋œ ์œ„์น˜(index)์— ์ฃผ์–ด์ง„ ์ปฌ๋ ‰์…˜์— ํฌํ•จ๋œ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถ”๊ฐ€. ์„ฑ๊ณตํ•˜๋ฉด true. ์‹คํŒจํ•˜๋ฉด false
void clear() LinkedList์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์‚ญ์ œ
boolean contains(Object c) ์ง€์ •๋œ ๊ฐ์ฒด๊ฐ€ LinkedList์— ํฌํ•จ๋˜์—ˆ๋Š”์ง€ ์•Œ๋ ค์คŒ
boolean containsAll(Collection c) ์ง€์ •๋œ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์—ˆ๋Š”์ง€ ์•Œ๋ ค์คŒ
Object get(int index) ์ง€์ •๋œ ์œ„์น˜(index)์˜ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
int indexOf(Object o) ์ง€์ •๋œ ๊ฐ์ฒด๊ฐ€ ์ €์žฅ๋œ ์œ„์น˜(์•ž์—์„œ ๋ช‡ ๋ฒˆ์งธ)๋ฅผ ๋ฐ˜ํ™˜
boolean isEmpty() LinkedList๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค. ๋น„์–ด์žˆ๋Š” true
Iterator iterator() iterator๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
int lastIndexOf(Object o) ์ง€์ •๋œ ๊ฐ์ฒด์˜ ์œ„์น˜(index)๋ฅผ ๋ฐ˜ํ™˜(๋๋ถ€ํ„ฐ ์—ญ์ˆœ๊ฒ€์ƒ‰)
ListIterator listIterator() ListIterator๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
ListIterator listIterator(int index) ์ง€์ •๋œ ์œ„์น˜์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ListIterator๋ฅผ ๋ฐ˜ํ™˜
Object remove(int index) ์ง€์ •๋œ ์œ„์น˜(index)์˜ ๊ฐ์ฒด๋ฅผ LinkedList์—์„œ ์ œ๊ฑฐ
boolean remove(Object o) ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ LinkedList์—์„œ ์ œ๊ฑฐ, ์„ฑ๊ณตํ•˜๋ฉด true. ์‹คํŒจํ•˜๋ฉด false
boolean removeAll(Collection c) ์ง€์ง•๋œ ์ปฌ๋ ‰์…˜์ด ์š”์†Œ์™€ ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์‚ญ์ œ
boolean retainAll(Collection c) ์ง€์ •๋œ ์ปฌ๋ ‰์…˜์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ
Object set(int index, Object element) ์ง€์ •๋œ ์œ„์น˜(index)์˜ ๊ฐ์ฒด๋ฅผ ์ฃผ์–ด์ง„ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟˆ
int size() LinkedList์— ์ €์žฅ๋œ ๊ฐ์ฒด์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
ListsubList(int fromIndex, Object element) LinkedList์˜ ์ผ๋ถ€๋ฅผ List๋กœ ๋ฐ˜ํ™˜
Object[] toArray() LinkedList์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ 
Object[] toArray(Object[] a) LinkedList์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜์—ฌ ๋ฐ˜ํ™˜
Object element() LinkedList์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
boolean offer(Object o) ์ง€์ •๋œ ๊ฐ์ฒด(o)๋ฅผ LinkedList์˜ ๋์— ์ถ”๊ฐ€. ์„ฑ๊ณตํ•˜๋ฉด true, ์‹คํŒจํ•˜๋ฉด false
Object peek() LinkedList์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
Object poll() LinkedList์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
Object remove() LinkedList์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐ
void addFirst(Object o) LinkedList์˜ ๋งจ ์•ž์— ๊ฐ์ฒด(o)๋ฅผ ์ถ”๊ฐ€
void addLast(Object o) LinkedList์˜ ๋งจ ๋์— ๊ฐ์ฒด(o)๋ฅผ ์ถ”๊ฐ€
Iterator descendingIterator() ์—ญ์ˆœ์œผ๋กœ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•œ DescendingIterator๋ฅผ ๋ฐ˜ํ™˜
Object getFirst() LinkedList์˜ ์ฒซ ๋ฒˆ์žฌ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
Object getLast() LinkedList์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
boolean offerFirst(Object o) LinkedList์˜ ๋งจ ์•ž์— ๊ฐ์ฒด(o)๋ฅผ ์ถ”๊ฐ€. ์„ฑ๊ณตํ•˜๋ฉด true
boolean offerLast(Object o) LinkedList์˜ ๋งจ ๊ธ‘์— ๊ฐ์ฒด(o)๋ฅผ ์ถ”๊ฐ€. ์„ฑ๊ณตํ•˜๋ฉด true
Object peekFirst() LinkedList์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
Object peekLast() LinkedList์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
Object pollFirst() LinkedList์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ์ œ๊ฑฐ
Object pollLast() LinkedList์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ์ œ๊ฑฐ
Object pop() removeFirst()์™€ ๋™์ผ
void push(Object o) addFirst()์™€ ๋™์ผ
Object removeFirest() LinkedList์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ œ๊ฑฐ
Object removeLast() LinkedList์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐ
boolean removeFirstOccurrence(Object o) LinkedList์—์„œ ์ณ‡๋ฒˆ์งธ๋กœ ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐ
boolean removeLastOccurrence(Object o) LinkedList์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ผ์น˜ํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐ

 

LinkedList ์™€ ArrayList ๋น„๊ต

  • ์ˆœ์ฐจ์ ์œผ๋กœ(๋งˆ์ง€๋ง‰ ์š”์†Œ๋ถ€ํ„ฐ ์—ญ์ˆœ์œผ๋กœ) ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ArrayList๊ฐ€ LinkedList๋ณด๋‹ค ๋น ๋ฅด๋‹ค
    • ArrayList๋Š” ๋งˆ์ง€๋ง‰ ๋ฐ์ดํ„ฐ๋ถ€ํ„ฐ ์‚ญ์ œํ•  ๊ฒฝ์šฐ ๊ฐ ์š”์†Œ๋“ค์˜ ์žฌ๋ฐฐ์น˜๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฆ„. ๋‹จ์ง€ ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ ๊ฐ’์„ null๋กœ ๋ฐ”๊พธ๊ธฐ๋งŒ ํ•˜๋ฉด๋˜๋‹ˆ๊นŒ.
  • ์ค‘๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€/์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” LinkedList๊ฐ€ ArrayList๋ณด๋‹ค ๋น ๋ฅด๋‹ค
    • LinkedList๋Š” ๊ฐ ์š”์†Œ๊ฐ„์˜ ์—ฐ๊ฒฐ๋งŒ ๋ณ€๊ฒฝํ•ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋น ๋ฆ„
    • ArrayList๋Š” ๊ฐ ์š”์†Œ๋“ค์„ ์žฌ๋ฐฐ์น˜ ํ•˜์—ฌ ์ถ”๊ฐ€ํ•  ๊ณต๊ฐ„์„ ํ™•๋ณดํ•˜๊ฑฐ๋‚˜ ๋นˆ ๊ณต๊ฐ„์„ ์ฑ„์›Œ์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌ์†๋„๊ฐ€ ๋Šฆ๋‹ค.
์ปฌ๋ ‰์…˜ ์ฝ๊ธฐ(์ ‘๊ทผ์‹œ๊ฐ„ ์ถ”๊ฐ€/์‚ญ์ œ ๋น„๊ณ 
ArrayList ๋น ๋ฅด๋‹ค ๋Š๋ฆฌ๋‹ค ์ˆœ์ฐจ์ ์ธ ์ถ”๊ฐ€์‚ญ์ œ๋Š” ๋” ๋น ๋ฆ„
๋น„ํšจ์œจ์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ
LinkedList ๋Š๋ฆฌ๋‹ค ๋น ๋ฅด๋‹ค ๋ฐ์–ดํƒ€๊ฐ€ ๋งŽ์„์ˆ˜๋ก ์ ‘๊ทผ์„ฑ์ด ๋–จ์–ด์ง

 

๐Ÿ›’ Stack

  • Last In First Out(LIFO)
  • ํ™œ์šฉ ์˜ˆ
    • ์ˆ˜์‹ ๊ณ„์‚ฐ
    • ์ˆ˜์‹๊ด„ํ˜ธ๊ฒ€์‚ฌ
    • ์›Œ๋“œํ”„๋กœ์„ธ์„œ์˜ undo/redo
    • ์›น๋ธŒ๋ผ์šฐ์ €์˜ ๋’ค๋กœ/์•ž์œผ๋กœ

์Šคํƒ์˜ ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ์„ค๋ช…
boolan empty() Stack์ด ๋น„์–ด์žˆ๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.
Object peek() Stack์˜ ๋งจ ์œ„์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜. pop()๊ณผ ๋‹ฌ๋ฆฌ Stack์—์„œ ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์ง€๋Š” ์•Š์Œ.(๋น„์—ˆ์„ ๋•Œ๋Š” EmptyStackException ๋ฐœ์ƒ)
Object pop() Stack์˜ ๋งจ ์œ„์— ์ €์žฅ๋œ ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ธ๋‹ค. (๋น„์—ˆ์„ ๋•Œ๋Š” EmptyStack
Object push(Object Item) Stack์— ๊ฐ์ฒด(item)๋ฅผ ์ €์žฅํ•œ๋‹ค.
int search(Object o) Stack์—์„œ ์ฃผ์–ด์ง„ ๊ฐ์ฒด(o)๋ฅผ ์ฐพ์•„์„œ ๊ทธ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜. ๋ชป์ฐพ์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜. (๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์œ„์น˜๋Š” 0์ด ์•„๋‹Œ 1๋ถ€ํ„ฐ ์‹œ์ž‘)

๐ŸŽน Queue

  • First In First Out(First In First Out)
  • ํ™œ์šฉ ์˜ˆ
    • ์ตœ๊ทผ ์‚ฌ์šฉ ๋ฌธ์„œ
    • ์ธ์‡„์ž‘์—… ๋Œ€๊ธฐ๋ชฉ๋ก
    • ๋ฒ„ํผ(buffer)
๋ฉ”์„œ๋“œ ์„ค๋ช…
boolean add(Object o) ์ง€์ •๋œ ๊ฐ์ฒด๋ฅผ Queue์— ์ถ”๊ฐ€ํ•œ๋‹ค. ์„ฑ๊ณตํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜, ์ €์žฅ๊ณต๊ฐ„์ด ๋ถ€์กฑํ•˜๋ฉด IlegalStateException ๋ฐœ์ƒ
Object remove() Queue์—์„œ ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด ๋ฐ˜ํ™˜. ๋น„์–ด์žˆ์œผ๋ฉด NoSuchElementException ๋ฐœ์ƒ
Object element ์‚ญ์ œ ์—†์ด ์š”์†Œ๋ฅผ ์ฝ์–ด์˜จ๋‹ค. peek์™€ ๋‹ฌ๋ฆฌ Queue๊ฐ€ ๋น„์—ˆ์„ ๋•Œ NoSuchElementException ๋ฐœ์ƒ
boolean offer(Object o) Queue์— ๊ฐ์ฒด๋ฅผ ์ €์žฅ. ์„ฑ๊ณตํ•˜๋ฉด true, ์‹คํŒจํ•˜๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
Object poll() Queue์— ๊ฐ์ฒด๋ฅผ ๊บผ๋‚ด์„œ ๋ฐ˜ํ™˜. ๋น„์–ด์žˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜
Object peek() ์‚ญ์ œ์—†์ด ์š”์†Œ๋ฅผ ์ฝ์–ด์˜จ๋‹ค. Queue๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉฐ null์„ ๋ฐ˜ํ™˜

PriorityQueue

  • Queue ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด ์ค‘ ํ•˜๋‚˜
  • ์ €์žฅํ•œ ์ˆœ์„œ์— ๊ด€๊ณ„์—†์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ฒƒ๋ถ€ํ„ฐ ๊บผ๋‚ธ๋‹ค
  • null์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค(NullPointerException)
  • ์ €์žฅ๊ณต๊ฐ„์œผ๋กœ ๋ฐฐ์—ด ์‚ฌ์šฉ
  • ํž™(heap)์ด๋ผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ํ˜•ํƒœ๋กœ ์ €์žฅ
  • ๊ฐ€์žฅ ํฐ ๊ฐ’์ด๋‚˜ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

Deque (Double-Enbed Queue)

  • Queue์˜ ๋ณ€ํ˜•
  • ์–‘ ์ชฝ ๋์— ์ถ”๊ฐ€/์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅ (Queue๋Š” ํ•œ ์ชฝ ๋์œผ๋กœ๋งŒ ์ถ”๊ฐ€/ ์‚ญ์ œ ๊ฐ€๋Šฅ)
  • Duque์˜ ์กฐ์ƒ์ด Queue
  • ๊ตฌํ˜„์ฒด๋Š” ArrayDeque, LinkedList

๋ฑ(Deque)์˜ ๋ฉ”์„œ๋“œ์— ๋Œ€์‘ํ•˜๋Š” ํ์™€ ์Šคํƒ์˜ ๋ฉ”์„œ๋“œ

Deuque Queue Stack
offerLast() offer() push()
pollLast()   pop()
pollFirst() poll()  
peekFirst() peek()  
paeekLast()   peek()

๐Ÿ˜ต ๊ณผ์ œ

LinkedList๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”

  • ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ListNode ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • ListNode add(ListNode head, ListNode nodeToadd, int position)์„ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • ListNode remove(ListNode head, int positionToRemove)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • boolean contains(ListNode head, ListNode nodeToCheck)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
public class ListNode {
    int data;
    ListNode next;

    public ListNode() {}
    public ListNode(int data) {
        this.data = data;
        this.next = null;
    }

    public ListNode add(ListNode head, ListNode nodeToAdd, int position) {
        ListNode node = head;

        //position ์ด์ „๊นŒ์ง€ ํƒ์ƒ‰
        for (int i = 0; i < position - 1; i++) {
            node = node.next;
        }

        //์ง€์ • ์œ„์น˜์— ๋…ธ๋“œ ์‚ฝ์ž…
        nodeToAdd.next = node.next;
        node.next = nodeToAdd;
        return head;
    }

    public ListNode remove(ListNode head, int positionToRemove) {
        ListNode node = head;

        //์‚ญ์ œ ์œ„์น˜๊ฐ€ ๊ฐ€์žฅ ์•ž์ธ๊ฒฝ์šฐ
        if(positionToRemove == 0){
            ListNode deleteToNode = node;
            head = node.next;
            deleteToNode.next = null;
            return deleteToNode;
        }
        for (int i = 0; i < positionToRemove - 1; i++) {
            node = node.next;
        }

        //์ง€์ • ์œ„์น˜ ๋…ธ๋“œ ์‚ญ์ œ
        ListNode deleteToNode = node.next;
        node.next = deleteToNode.next;
        deleteToNode.next = null;
        return deleteToNode;
    }

    public boolean contains(ListNode head, ListNode nodeToCheck) {
        while (head != null) {
            if (head.data == nodeToCheck.data) {
                return true;
            }
            head = head.next;
        }
        return false;
    }

    public void print(ListNode node) {
        while (node != null) {
            System.out.print(node.data);
            node = node.next;
        }
    }
}

Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”

  • int ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ์ •์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”
public class Stack {
    private int top = 0;
    private int StackArr[];

    public Stack(int size) {
        StackArr = new int[size];
    }

    public void push(int data) {
        top++;
        StackArr[top] = data;
    }

    public int pop() {
        int ret = StackArr[top];
        top--;
        return ret;
    }

    public void print() {
        for (int i = top; i > 0; i--) {
            System.out.println("|" + StackArr[i] + "|");
        }
        System.out.println("ใ…ก");
    }
}

์•ž์„œ ๋งŒ๋“  ListNode๋ฅผ ์‚ฌ์šฉํ•ด์„œ Stack์„ ๊ตฌํ˜„ํ•˜์„ธ์š”

  • ListNode head๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ListNodeStack ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • void push(int data)๋ฅผ ๊ตฌํ˜„ํ•˜์„ธ์š”
  • int pop()์„ ๊ตฌํ˜„ํ•˜์„ธ์š”
public class ListNodeStack {
    int top;
    ListNode node;

    public ListNodeStack() {
        this.node = null;
        this.top = -1;
    }

    public void push(int data) {
        ListNode pushNode = new ListNode(data);
        if (node == null) {
            node = new ListNode(data);
            top++;
        } else {
            node = node.add(node, pushNode, ++top);
        }
    }

    public int pop() {
        if (top == -1) {
            System.out.println("Empty");
            return top;
        }
        return node.remove(node, top--).data;
    }

    public void print(ListNode node) {
        while (node != null) {
            System.out.print(node.data);
            node = node.next;
        }
    }
}

Seize the day!

Spring MVC | Spring Boot | Spring Security | Mysql | Oracle | PostgreSQL | Vue.js | Nuxt.js | React.js | TypeScript | JSP | Frontend | Backend | Full Stack | ์ž๊ธฐ๊ณ„๋ฐœ | ๋ฏธ๋ผํด ๋ชจ๋‹ | ์ผ์ƒ