List, ArrayList, LinkedList BigO, DI
โ Array
- ๐๐ป use index and input, change, get value:
O(1)
- ๋ฐฐ์ด์๋ index๊ฐ ์๊ธฐ ๋๋ฌธ์
- ๐๐ป search, insert value:
O(n)
, need to search, compare the whole array, ๐๐ป array size is fixed, need to fix array size when creating array
- input value
array[100] = 1;
: O(1) - change value
array[100] = 10;
: O(1) - get value
array[100]
: O(1) - search value
if(array[i] == 10)
: O(n) - insert certain value in the middle of array: O(n), ์์ผ๋ก ํ ์นธ์ฉ ๋ค ๋ฐ์ด์ผ ํจ
โ How does index help get/change value fast?
- in array, only one type of data is possible
int[]
,String[]
- each dataset has same size
int has 4 byte
- so
memory ์์ ์์น + index * 4byte
memory ์์ ์์น
: x100int
: 4byteindex[2]์ ์์น
: x100 + 4byte * 2
๐ก BigO
์๊ณ ๋ฆฌ์ฆ์ ๋ฐ์ดํฐ ํฌ๊ธฐ ๋ณํ์ ๋ฐ๋ฅธ ์ฑ๋ฅ ๋ณํ ๊ฒฝํฅ์ ์๋ ค์ค๋ค
- O(1): find with one operation, ํ๋ฒ๋ง์ ์ฐพ์
- O(n):
n
number of data,n
operations - O(n2):
n
number of data, much moren*n
operations - O(log n): more than
O(1)
, but less thanO(n)
- tree
- O(n log n): like ` O(log n)
, but multiplied by
n`
โ List
- flexible size
- duplication allowed
index
- List ๐ Array: List has flexible size
โ ArrayList
use array
flexible size
- ๐๐ป flexible size
- if more space is needed, create a new arraylist with more space and copy paste the existing old array
default capacity
:10- if exceeds
default capacity
, increase capacity by50%
- ๊ธฐ๋ณธ ํฌ๊ธฐ๋ 10, 11๋ฒ์งธ ๋ฐ์ดํฐ ์ถ๊ฐ โก๏ธ ํฌ๊ธฐ 15 โก๏ธ 22 โก๏ธ 33โฆ
- then copy old arraylist with
System.arraycopy()
- if more space is needed, create a new arraylist with more space and copy paste the existing old array
- ๐๐ป some space of arraylist might not be used โก๏ธ waste memory
- ํ์ง๋ง
arraylist
๋ฅผ ๋ด๊ฐ ํ์ํ ๋ฐ์ดํฐ ํฌ๊ธฐ์ ๋ฑ ๋ง๊ฒ ๋ง๋๋ ๊ฑด ์๋๋ฏ๋ก arraylist
์ ๋จ๋ ๊ณต๊ฐ์ด ์๊ธธ ์ ์์- ๋ฐ์ดํฐ
11
๋ง ์ถ๊ฐํ๋ฉด12~15
๋ ๋จ๋ ๊ณต๊ฐ
- ํ์ง๋ง
- ๐๐ป get value with index, add or delete last value is
O(1)
- ๐๐ป search, add or delete in the first or middle of array is
O(n)
(์์ผ๋ก ํ ์นธ์ฉ ๋ค ๋ฐ์ด์ผ ํจ)
- add/delete data last of array: O(1)
- add/delete data middle or front: O(n)
- get value with index: O(1)
- search: O(n)
โ array ๋๋ array list์์ ๋ฐ์ดํฐ ์ค๊ฐ ์ฝ์ /์ญ์ ์ฐ์ฐ์ด ๋ ๋ถ๋ถ ์ฝ์ /์ญ์ ๋ณด๋ค ์ ์ผ๋ฐ์ ์ผ๋ก ๋นํจ์จ์ ์ผ๊น์?
- ์ค๊ฐ์ ์ฝ์ ๋/๋น ์ง ๋ฐ์ดํฐ ์ธ์ ๋๋จธ์ง๋ฅผ ์ด๋์์ผ์ผ ํด์
โ ArrayList and generic
- use generic in array list to ensure type safety
- in one
array list
, only one type of data
1
2
3
4
5
6
7
8
9
10
11
//๐๐ป Before
MyArrayList list = new MyArrayList();
list.add(1);
list.add("hello");
//list: [1, "hello"] //Integer, String์ด ํผํฉ๋์ด ์์
//๐๐ป After
MyArrayList<Inetger> list = new MyArrayList(); //use generic
list.add(1);
list.add("hello"); //๐ด compile error
//๐๐ป list์๋ Integer๋ง ๋ค์ด๊ฐ ์ ์๊ฒ ๋จ
๐ก Node
node = value โ next node reference address
- node๋ผ๋ฆฌ ์ฐ๊ฒฐ
์ node๋ ๋ค์ node์ ์ฃผ์๋ฅผ ๊ฐ์ง๊ณ ์์
- ๐๐ป ํ์ํ ํฌ๊ธฐ๋งํผ๋ง ๋ฉ๋ชจ๋ฆฌ ํ๋ณด, no memory waste
- ๐๐ป add/delete middle/front data efficiently
โ Linked List
has order, allow duplication
use node
reference before, next node
know first, last node
- ๐๐ป linked list size is size of data, no memory waste
- ๐๐ป add/delete front, middle, last:
O(1)
, only has to change surrounding node references(address) - ๐๐ป search data with specific index, has to search whole linked list โก๏ธ
O(n)
๐ก Dependency Injection
OCP
: client code should be closed, main code should be openDependency Injection
:- postpoone for dependency injection until main code is RUN time
- open to what specific class will be injected:
OCP
- client code does not change, postpone so that main code will decide:
OCP
- use interface
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
//โ๏ธ interface MyList
public interface MyList <E>{}
//โ๏ธ MyList is implemented by MyArrayList and MyLinkedList
// โญ๏ธ depend on interface MyList
public class MyArrayList<E> implements MyList<E>{}
public class MyLinkedList<E> implements MyList<E> {}
//โ๏ธ client code
public class BatchProcessor {
//use interface, not specific class such as MyArrayList or MyLinkedList
//DO NOT DEPEND on a specific class
//POSTPONE
// โญ๏ธ depend on interface MyList
private final MyList<Integer> list;
public BatchProcessor(MyList<Integer> list) {
this.list = list;
}
}
//โ๏ธ main class to be run
main(){
//decide here, in main code which class to run
//dependency injection
BatchProcessor arrayBatchProcessor = new BatchProcessor(new MyArrayList<Integer>());
}
BatchProcessor(client code)
DOES NOT depend on a specific classBatchProcessor(client code)
depends on interface,MyList
- specific class
MyArrayList
,MyLinkedList
depends on interface,MyList
main class(class to be run)
decides which specific class will be run- specific class:
MyArrayList
,MyLinkedList
- specific class:
- POSTPONE deciding specific class โก๏ธ dependency injection
OCP
: client code does not change, main class is modifiedpolymorphism
: several classes could be injected to main classapplication is loosely coupled, extendable, maintainable
- ๋ฉ์ธ ํด๋ผ์ค๊ฐ ์ด๋ค ๊ตฌ์ฒด ํด๋์ค๋ฅผ ์คํํ ์ง ๋ฐํ์๊น์ง ๊ฒฐ์ ์ ๋ฏธ๋ฃธ, ์ง์ฐ
- ํด๋ผ์ด์ธํธ ํด๋ผ์ค๋ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉ, ์ธํฐํ์ด์ค์ ์์กด โก๏ธ ์์กด์ฑ ์ฃผ์
- ๊ตฌ์ฒด ํด๋์ค๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํ(์์กด) โก๏ธ ์์กด์ฑ ์ฃผ์
- ์ด๋ค ํด๋์ค๋ฅผ ์คํํ ์ง ๋ฉ์ธํด๋์ค์๊ฒ ์๋ ค์ค์ผ ํจ
โ๏ธ compile time dependency
- dependency injection between class
BatchProcessor(client code)
depends onMyList(interface)
- specific class
MyArrayList
,MyLinkedList
depends onMyList(interface)
- implements interface
โ๏ธ runtime dependency
- dependency injection between instance
1
2
MyLinkedList<Integer> list = new MyLinkedList(); //x002
BatchProcessor processor = new BatchProcessor(list); //reference x002
โ
โ
โ
โ
โ
This post is licensed under CC BY 4.0 by the author.