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.