[์ปฌ๋ ์ ํ๋ ์์ํฌ] List
1. ๋ฆฌ์คํธ๋ ๋ฌด์์ธ๊ฐ?
โ๏ธ ๋ฆฌ์คํธ์ ํน์ง
- ์์ ๋ณด์ฅ: ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ์์๋ฅผ ์ ์ง
- ์ค๋ณต ํ์ฉ: ๊ฐ์ ๊ฐ์ ์ฌ๋ฌ ๋ฒ ์ ์ฅ ๊ฐ๋ฅ
- ๋ฐฐ์ด๊ณผ ๋น์ทํ์ง๋ง ํฌ๊ธฐ๊ฐ ์๋์ผ๋ก ์กฐ์ ๋จ
2. ์ง์ ๊ตฌํํ ๋ฆฌ์คํธ
โ๏ธ ์๋ฐ์์ ์ ๊ณตํ๋ ArrayList, LinkedList๋ฅผ ํ๋ด๋ด์ด ๊ธฐ๋ณธ์ ์ธ ๋ฆฌ์คํธ ๊ตฌ์กฐ๋ฅผ ์ง์ ๊ตฌํํด๋ณด์.
โฐ ๊ณตํต ์ธํฐํ์ด์ค : MyList
- ์ด ์ธํฐํ์ด์ค ๋๋ถ์, MyArrayList๋ MyLinkedList๋ฅผ ๋์ผํ ๋ฐฉ์์ผ๋ก ์ฌ์ฉํ ์ ์๊ฒ ๋์๋ค.
public interface MyList<E> {
int size();
void add(E e);
void add(int index, E e);
E get(int index);
E set(int index, E element);
E remove(int index);
int indexOf(E o);
}
3. MyArrayList - ๋ฐฐ์ด ๊ธฐ๋ฐ ๋ฆฌ์คํธ
โ๏ธ ๋ด๋ถ ๋ฐฐ์ด ์ฌ์ฉ
โ๏ธ ๋ฐฐ์ด ํฌ๊ธฐ๊ฐ ์ด๊ณผํ๋ฉด grow() ๋ฉ์๋๋ก 2๋ฐฐ ํ์ฅ
โ๏ธ ์ฝ์ ์ด๋ ์ญ์ ์ ๋ฐฐ์ด ์์๋ฅผ ๋ฐ๊ฑฐ๋ ๋น๊ฒจ์ผ ํด์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆผ
@Override
public void add(int index, E e) {
if (size == elementData.length) grow();
shiftRightFrom(index);
elementData[index] = e;
size++;
}
| ์์ | ์๊ฐ |
| ์์ ์ถ๊ฐ | O(n) |
| ๋ค์ ์ถ๊ฐ | O(1) |
| ์กฐํ | O(1) |
4. MyLinkedList - ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ธฐ๋ฐ
โ๏ธ ๋ ธ๋(Node)๋ฅผ ์ด์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ฐ๊ฒฐ
โ๏ธ ๋ฐฐ์ด์ฒ๋ผ ํ ๋ฒ์ ์ ๊ทผ ๋ถ๊ฐ -> ๋ ธ๋๋ฅผ ์ํํด์ผ ํ๋ค.
โ๏ธ But, ์ฝ์ /์ญ์ ๋ ๋น ๋ฅด๋ค (์ฐธ์กฐ๋ง ๋ณ๊ฒฝํ๋ฉด ๋จ!)
@Override
public void add(int index, E e) {
Node<E> newNode = new Node<>(e);
if (index == 0) {
newNode.next = first;
first = newNode;
} else {
Node<E> prev = getNode(index - 1);
newNode.next = prev.next;
prev.next = newNode;
}
size++;
}
| ์์ | ์๊ฐ |
| ์์ ์ถ๊ฐ | O(1) |
| ์ค๊ฐ ์ถ๊ฐ | O(n) |
| ์กฐํ | O(n) |
5. ๋คํ์ฑ & ์์กด๊ด๊ณ ์ฃผ์
โ๏ธ ๋ฆฌ์คํธ ์ ๋ต์ ๋ฐ๊พธ๊ธฐ ์ํด MyList ์ธํฐํ์ด์ค๋ฅผ ํ์ฉํ ์์กด๊ด๊ณ ์ฃผ์ ์ ์ ์ฉํ ์ ์๋ค
public class BatchProcessor {
private final MyArrayList<Integer> list = new MyArrayList<>();
}
public class BatchProcessor {
private final MyList<Integer> list;
public BatchProcessor(MyList<Integer> list) {
this.list = list;
}
public void logic(int size) {
for (int i = 0; i < size; i++) {
list.add(0, i);
}
}
}
โ MyArrayList, MyLinkedList ์ค ์ด๋ค ๊ตฌํ์ฒด๋ฅผ ์ฌ์ฉํ ์ง๋ ์์ฑ์ ์ฃผ์ ์ผ๋ก ๋ฐํ์์ ๊ฒฐ์ ํ ์ ์๊ฒ ๋์๋ค!
6. ์ฑ๋ฅ ๋น๊ต : ์ง์ ํ ์คํธ ํด๋ณด๊ธฐ
โ๏ธ ์์ ์ถ๊ฐ
- MyArrayList: O(n) → ๋ชจ๋ ์์๋ฅผ ํ ์นธ์ฉ ๋ฐ์ด์ผ ํจ
- MyLinkedList: O(1) → ์ฐธ์กฐ๋ง ๋ฐ๊พธ๋ฉด ๋จ
for (int i = 0; i < size; i++) {
list.add(0, i);
}
โ๏ธ ์ค๊ฐ ์ถ๊ฐ
- ๋ ๋ค ํ๊ท ์ ์ผ๋ก๋ O(n)
- ํ์ง๋ง MyArrayList๋ ๋ด๋ถ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ๋ณต์ฌ๋ฅผ ๋ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌ
โ๏ธ ๋ค์ ์ถ๊ฐ
- MyArrayList: O(1) (๋จ, ๊ฐ๋ grow)
- MyLinkedList: O(n) (๋ง์ง๋ง ๋ ธ๋ ํ์ ํ์)
7. ์๋ฐ ์ปฌ๋ ์ ํ๋ ์์ํฌ์ List
โ๏ธ ์๋ฐ๋ ๊ธฐ๋ณธ์ ์ผ๋ก List ๊ตฌํ์ฒด๋ฅผ ์ ๊ณตํ๋ค.
โ๏ธ ์ค๋ฌด์์๋ ๋๋ถ๋ถ ArrayList๋ฅผ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉํ๋ค
โ๏ธ ์์ชฝ์์ ์์ฃผ ์ถ๊ฐ/์ญ์ ํ๋ ๊ฒฝ์ฐ์๋ LinkedList๋ ๊ณ ๋ คํด๋ณผ๋ง ํ๋ค.
| ๊ธฐ๋ฅ | ArrayList | LinkedList |
| ์์ ์ถ๊ฐ | O(n) | O(1) |
| ๋ค์ ์ถ๊ฐ | O(1) | O(1) |
| ์กฐํ | O(1) | O(n) |
| ๊ฒ์ | O(n) | O(n) |
8. ์ ๋ฆฌ
โ๏ธ List๋ ์์๋ฅผ ๋ณด์ฅํ๊ณ ์ค๋ณต์ ํ์ฉํ๋ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
โ๏ธ ArrayList๋ ๋ฐฐ์ด ๊ธฐ๋ฐ, LinkedList๋ ๋ ธ๋ ๊ธฐ๋ฐ
โ๏ธ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ์ค๊ณ๋ฅผ ํ๋ฉด ๋ค์ํ ๊ตฌํ์ฒด๋ก ์ฝ๊ฒ ์ ๋ต์ ๋ฐ๊ฟ ์ ์๋ค.
โ๏ธ ์ค์ ์ฑ๋ฅ์ ๋จ์ํ ์๊ฐ ๋ณต์ก๋๋ฟ ์๋๋ผ CPU ์บ์, ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ ๋ฑ๋ ์ํฅ์ ๋ฏธ์น๋ค.
โ๏ธ ์๋ฐ์ ArrayList๋ ๋ฉ๋ชจ๋ฆฌ ๊ณ ์ ๋ณต์ฌ ๋ฑ ์ต์ ํ๊ฐ ์ ๋์ด ์์ด ์ค๋ฌด์์๋ ๋ ๋ง์ด ์ฐ์ธ๋ค.
<์ฐธ๊ณ > - ๊น์ํ์ ์ค์ ์๋ฐ ์ค๊ธ2ํธ ๊ฐ์ ์น์ 6. ์ปฌ๋ ์ ํ๋ ์์ํฌ List